当前位置:Linux教程 - Shell - shell - 如何用Bshell转换cgi传入的变量中的非ASCII字符(汉字)

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

如何用Bshell转换cgi传入的变量中的非ASCII字符(汉字)
2004-04-23 15:18 pm
来自:Linux文档
现载:Www.8s8s.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}
'