参考文档:http://www.linuxforum.net/books/UTF-8-Unicode.html
代码如下:
===========================================
复制代码 代码如下:
<scriptlanguage="VBScript">
'http://www.linuxforum.net/books/UTF-8-Unicode.html
PublicFunctionUTF8EncodeChar(z)
Dimc:c=AscW(z)'取UNICODE编码
ifc>0Andc<256Then'Asc编码直接返回
UTF8EncodeChar=z
ExitFunction
EndIf
Ifc<0Thenc=c+&H10000&'VBScript的Integer溢出,加上
Dimk:k=CLng(c)'备份一个编码,后面判断要用
Dimb()
Dimi:i=0
Whilec>&H0&'将编码按照6位一组,分组存到字节数组b中
ReDimPreserveb(i)
b(i)=CByte(cAnd&H3F&)
c=c&H40&
i=i+1
Wend
IfUBound(b)>0Then'如果分开的6位组不止一个,除最高一组外,全部加上二进制10000000
Fori=0ToUBound(b)-1
b(i)=b(i)+&H80
Next
EndIf
i=UBound(b)'根据字符的UNICODE编码范围,给最高组加上前缀
Ifk<=CLng(&H7F&)Then
b(i)=b(i)+0
ElseIfk<=CLng(&H7FF&)Then
b(i)=b(i)+&HC0
ElseIfk<=Clng(&HFFFF&)Then
b(i)=b(i)+&HE0
ElseIfk<=CLng(&H1FFFFF&)Then
b(i)=b(i)+&HF0
ElseIfk<=CLng(&H3FFFFFF&)Then
b(i)=b(i)+&HF8
Else
b(i)=b(i)+&HFC
EndIf
UTF8EncodeChar=""
Fori=UBound(b)To0Step-1'将分组转换成URL编码
UTF8EncodeChar=UTF8EncodeChar&"%"&Right("00"&Hex(b(i)),2)
Next
Eraseb
EndFunction
PublicFunctionUTF8EncodeString(s)
Dimi,l,c:l=Len(s)
Fori=1Tol
UTF8EncodeString=UTF8EncodeString&UTF8EncodeChar(Mid(s,i,1))
Next
EndFunction
MsgBoxUTF8EncodeString("圪圪eglic")
</script>
测试方法:
http://www.google.com/search?hl=zh-CN&newwindow=1&rls=GGLG%2CGGLG%3A2006-15%2CGGLG%3Azh-CN&q=你的编码
复制代码 代码如下:
functionrevertUTF8(szInput)
{
varx,wch,wch1,wch2,uch="",szRet="";
for(x=0;x<szInput.length;x++)
{
if(szInput.charAt(x)=="%")
{
wch=parseInt(szInput.charAt(++x)+szInput.charAt(++x),16);
if(!wch){break;}
if(!(wch&0x80))
{
wch=wch;
}
elseif(!(wch&0x20))
{
x++;
wch1=parseInt(szInput.charAt(++x)+szInput.charAt(++x),16);
wch=(wch&0x1F)<<6;
wch1=wch1&0x3F;
wch=wch+wch1;
}
else
{
x++;
wch1=parseInt(szInput.charAt(++x)+szInput.charAt(++x),16);
x++;
wch2=parseInt(szInput.charAt(++x)+szInput.charAt(++x),16);
wch=(wch&0x0F)<<12;
wch1=(wch1&0x3F)<<6;
wch2=(wch2&0x3F);
wch=wch+wch1+wch2;
}
szRet+=String.fromCharCode(wch);
}
else
{
szRet+=szInput.charAt(x);
}
}
return(szRet);
}
functionu2utf8($c)
{
/*for($i=0;$i<count($c);$i++)*/
$str="";
if($c<0x80){
$str.=$c;
}
elseif($c<0x800){
$str.=chr(0xC0|$c>>6);
$str.=chr(0x80|$c&0x3F);
}
elseif($c<0x10000){
$str.=chr(0xE0|$c>>12);
$str.=chr(0x80|$c>>6&0x3F);
$str.=chr(0x80|$c&0x3F);
}
elseif($c<0x200000){
$str.=chr(0xF0|$c>>18);
$str.=chr(0x80|$c>>12&0x3F);
$str.=chr(0x80|$c>>6&0x3F);
$str.=chr(0x80|$c&0x3F);
}
return$str;
}
当前1/2页12下一页阅读全文