[Code] <SCRIPT>
Function chinesefromutf8url (strutf8)
{
VaR BSTR = "";
VaR noffset = 0;
// Processing point on strutf8
If (strutf8 = "")
Return "";
Strutf8 = strutf8.tolowercase ();
Noffset = strutf8.indexof ("% E ");
If (noffset =-1)
Return strutf8;
While (noffset! =-1)
{
BSTR + = strutf8.substr (0, noffset );
Strutf8 = strutf8.substr (noffset, strutf8.length-noffset );
If (strutf8 = "" returns invalid strutf8.length <9) // bad string
Return BSTR;
BSTR + = utf8codetochinesechar (strutf8.substr (0, 9 ));
Strutf8 = strutf8.substr (9, strutf8.length-9 );
Noffset = strutf8.indexof ("% E ");
}
Return BSTR + strutf8;
}
Function unicodefromutf8 (strutf8)
{
VaR BSTR = "";
VaR ntotalchars = strutf8.length; // total chars to be processed.
VaR noffset = 0; // processing point on strutf8
VaR nremainingbytes = ntotalchars; // how many bytes left to be converted
VaR noutputposition = 0;
VaR icode, icode1, icode2; // The value of the Unicode.
While (noffset <ntotalchars)
{
Icode = strutf8.charcodeat (noffset );
If (icode & 0x80) = 0) // 1 byte.
{
If (nremainingbytes <1) // not enough data
Break;
BSTR + = string. fromcharcode (icode & 0x7f );
Noffset ++;
Nremainingbytes-= 1;
}
Else if (icode & 0xe0) = 0xc0) // 2 bytes
{
Icode1 = strutf8.charcodeat (noffset + 1 );
If (nremainingbytes <2 then bytes // not enough data
(Icode1 & 0xc0 )! = 0x80) // invalid Pattern
{
Break;
}
BSTR + = string. fromcharcode (icode & 0x3f) <6) encode (icode1 & 0x3f ));
Noffset + = 2;
Nremainingbytes-= 2;
}
Else if (icode & 0xf0) = 0xe0) // 3 bytes
{
Icode1 = strutf8.charcodeat (noffset + 1 );
Icode2 = strutf8.charcodeat (noffset + 2 );
If (nremainingbytes <3 then bytes // not enough data
(Icode1 & 0xc0 )! = 0x80 then specify // invalid Pattern
(Icode2 & 0xc0 )! = 0x80)
{
Break;
}
BSTR + = string. fromcharcode (icode & 0x0f) <12) Encoding
(Icode1 & 0x3f) <6) Complete
(Icode2 & 0x3f ));
Noffset + = 3;
Nremainingbytes-= 3;
}
Else // 4 or more bytes -- unsupported
Break;
}
If (nremainingbytes! = 0)
{
// Bad utf8 string.
Return "";
}
Return BSTR;
}
Function utf8codetochinesechar (strutf8)
{
VaR icode, icode1, icode2;
Icode = parseint ("0x" + strutf8.substr (1, 2 ));
Icode1 = parseint ("0x" + strutf8.substr (4, 2 ));
Icode2 = parseint ("0x" + strutf8.substr (7, 2 ));
Return string. fromcharcode (icode & 0x0f) <12) Encoding
(Icode1 & 0x3f) <6) Complete
(Icode2 & 0x3f ));
}
Alert (chinesefromutf8url ("% E6 % B5 % 8B % E8 % af % 95 "))
</SCRIPT> [Code]