How does JavaScript solve UTF8 encoding?

Source: Internet
Author: User
Tags 0xc0 return
Javascript| Code <script>
Alert (Chinesefromutf8url ("%e4%b8%ad%e6%96%87"))
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) |
((ICode1 & 0x3F) << 6) |
(ICode2 & 0x3F));
}

Converts the Chinese portion of the UTF8 encoded URL (< 0xFFFF).
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 = = "" | | 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 | |//Not enough data
(ICode1 & 0xc0)!= 0x80)//Invalid pattern
{
Break
}

BSTR + + String.fromCharCode ((Icode & 0x3F) << 6) | (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 | |//Not enough data
(ICode1 & 0xc0)!= 0x80 | | Invalid pattern
(ICode2 & 0xc0)!= 0x80)
{
Break
}

BSTR + + String.fromCharCode ((Icode & 0x0f) << 12) |
((ICode1 & 0x3F) << 6) |
(ICode2 & 0x3F));
Noffset + 3;
Nremainingbytes-= 3;
}
else//4 or more bytes-unsupported
Break
}

if (nremainingbytes!= 0)
{
Bad UTF8 string.
Return "";
}

Return BSTR;
}
</script>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.