参考文档:http://www.linuxforum.net/books/UTF-8-Unicode.html
代码如下:
===========================================
代码如下:
<script language="VBScript">
''http://www.linuxforum.net/books/UTF-8-Unicode.html
Public Function UTF8EncodeChar(z)
Dim c : c=AscW(z)''取UNICODE编码
if c>0 And c<256 Then''Asc编码直接返回
UTF8EncodeChar=z
Exit Function
End If
If c<0 Then c=c + &H10000&''VBScript的Integer溢出,加上
Dim k : k=CLng(c)''备份一个编码,后面判断要用
Dim b()
Dim i : i=0
While c>&H0&''将编码按照6位一组,分组存到字节数组 b 中
ReDim Preserve b(i)
b(i)=CByte(c And &H3F&)
c=c \ &H40&
i=i+1
Wend
If UBound(b)>0 Then ''如果分开的6位组不止一个,除最高一组外,全部加上二进制10000000
For i=0 To UBound(b)-1
b(i)=b(i) + &H80
Next
End If
i=UBound(b)''根据字符的UNICODE编码范围,给最高组加上前缀
If k<=CLng(&H7F&) Then
b(i) = b(i) + 0
ElseIf k<=CLng(&H7FF&) Then
b(i) = b(i) + &HC0
ElseIf k<=Clng(&HFFFF&) Then
b(i) = b(i) + &HE0
ElseIf k<=CLng(&H1FFFFF&) Then
b(i) = b(i) + &HF0
ElseIf k<=CLng(&H3FFFFFF&) Then
b(i) = b(i) + &HF8
Else
b(i) = b(i) + &HFC
End If
UTF8EncodeChar=""
For i=UBound(b) To 0 Step -1''将分组转换成URL编码
UTF8EncodeChar=UTF8EncodeChar & "%" & Right("00" & Hex(b(i)),2)
Next
Erase b
End Function
Public Function UTF8EncodeString(s)
Dim i,l,c : l=Len(s)
For i=1 To l
UTF8EncodeString=UTF8EncodeString & UTF8EncodeChar(Mid(s,i,1))
Next
End Function
MsgBox UTF8EncodeString("圪圪 eglic ")
</script>
测试方法:
http://www.google.com/search?hl=zh-CN&newwindow=1&rls=GGLG%2CGGLG%3A2006-15%2CGGLG%3Azh-CN&q=你的编码
代码如下:
function revertUTF8(szInput)
{
var x,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;
}
else if (!(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);
}
function u2utf8($c)
{
/*for($i=0;$i<count($c);$i++)*/
$str="";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=chr(0xC0 | $c>>6);
$str.=chr(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=chr(0xE0 | $c>>12);
$str.=chr(0x80 | $c>>6 & 0x3F);
$str.=chr(0x80 | $c & 0x3F);
}
else if ($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;
}
11
22
33