Reference: http://www.linuxforum.net/books/UTF-8-Unicode.html
The Code is as follows:
========================================================== ===Copy codeThe Code is as follows: <script language = "VBScript">
'Http: // www.linuxforum.net/books/4268-unicode.html
Public Function UTF8EncodeChar (z)
Dim c: c = AscW (z) 'uses UNICODE encoding
If c> 0 And c <256 then' Asc encoding is returned directly
UTF8EncodeChar = z
Exit Function
End If
If c <0 Then c = c + & H10000 & 'vbscript Integer overflow, add
Dim k: k = CLng (c) 'back up an encoding, which will be used for later determination
Dim B ()
Dim I: I = 0
While c> & H0 & 'groups the codes into 6-bit arrays and stores them in byte array B.
ReDim Preserve B (I)
B (I) = CByte (c And & H3F &)
C = c \ & H40 &
I = I + 1
Wend
If UBound (B)> 0 then', If there are more than one 6-bit group separated by the first group, add the binary 10000000
For I = 0 To UBound (B)-1
B (I) = B (I) + & H80
Next
End If
I = UBound (B) 'adds a prefix to the highest group based on the UNICODE encoding range of the character
If k <= CLng (& H7F &) Then
B (I) = B (I) + 0
ElseIf k <= CLng (& 0000ff &) 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) + & HFCs
End If
UTF8EncodeChar = ""
For I = UBound (B) To 0 Step-1 'convert the group into URL Encoding
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 ("invalid eglic ")
</Script>
Test method:
Http://www.google.com/search? Hl = zh-CN & newwindow = 1 & rls = GGLG % 2 CGGLG % 3A2006-15% 2 CGGLG % 3Azh-CN & q = your codeCopy codeThe Code is as follows: function revertUTF8 (szInput)
{
Var x, wch, wret, 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;
WF = wch1 & 0x3F;
Wch = wch + wch;
}
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;
WF = (wch1 & 0x3F) <6;
Wch2 = (wch2 & 0x3F );
Wch = wch + 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;
}