| 您的位置:首页 > 文档 > 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 【评论】 【加入收藏夹】 【大 中 小】 【打印】 【关闭】 ※ 相关链接
|