您的位置:首页 > 文档 > Shell >
 文章分类 

如何用Bshell转换cgi传入的变量中的非ASCII字符(汉字)


创建:2005-10-26 00:49:42
作者:Unlinux
来自: http://www.Unlinux.com

问题没说清楚.你要把汉字转换成什么?
应该不是要转换,而是要正确显示吧?

我是想把cgi传入参数中的(例如:%E0%E0| 原本是汉字的)值,用shell or awk 转换成汉字显示出来!

用下面的awk脚本:
echo "adsfkjladsjf%E0%E0"|urldecode
将输出:
assfkjladsjf噜

urldecode如下:(偶改造的)#!/bin/ksh -x
awk '
BEGIN {

hextab="0123456789ABCDEF"
for ( i=1; i<=255; ++i ) ord [i] = sprintf("%c",i);
}
{
decoded = ""
for ( i=1; i<=length ($0); ++i ) {
c = substr ($0, i, 1)
if ( c ~ /[a-zA-Z0-9.-]/ ) {
decoded = decoded c # safe character
} else if ( c == " " ) {
decoded = decoded "+" # special handling
} else if ( c == "%" ) {
hi= substr($0,i+1,1);
low=substr($0,i+2,1);
i++;i++
decoded = decoded ord[(index(hextab,hi)-1)*16+index(hextab,low)-
1]
}
}
END{print decoded}
'



转载自:http://www.unlinux.com/doc/shell/20051026/68.html

评论】 【加入收藏夹】 【 】 【打印】 【关闭
 *  请尊重我们的劳动,转载请注明出自UnLinux.Com及作者名 * 

※ 相关链接
 ·如何修改命令提示信息  (2005-10-26 00:49:42)
 ·如何取消beep声音  (2005-10-26 00:49:42)
 ·awk中如何使用shell的环境变量  (2005-10-26 00:49:41)
 ·awk中如何用print输出单引号  (2005-10-26 00:49:41)
 ·请问如何用Shell编: 在当前目录下保留指定日期的文件,其余的全部删除?  (2005-10-26 00:49:41)
 ·请问如何用shell作隔行删除  (2005-10-26 00:49:41)
 ·请问如何抽取特征字的下一行  (2005-10-26 00:49:41)

Copyright © 2005 UnLinux.Com All Rights Reserved