參考文檔: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;
}
當前1/2頁
12下一頁閱讀全文