Convert Chinese characters to UTF8 encoding

Source: Internet
Author: User
Tags eval pow
Code | chinese | Convert//--------convert Chinese characters to UTF8 encoding------------------------//
function EncodeUtf8 (S1)
{
var s = Escape (S1);
var sa = s.split ("%");
var Retv = "";
if (Sa[0]!= "")
{
Retv = sa[0];
}
for (var i = 1; i < sa.length i + +)
{
if (sa[i].substring (0,1) = = "U")
{
Retv + + Hex2utf8 (Str2hex (sa[i].substring (1,5)));

}
else Retv + = "%" + sa[i];
}

return RETV;
}
function Str2hex (s)
{
var c = "";
var n;
var ss = "0123456789ABCDEF";
var digs = "";
for (var i = 0; i < s.length i + +)
{
c = S.charat (i);
n = ss.indexof (c);
Digs + = Dec2dig (eval (n));

}
return value;
return digs;
}
function Dec2dig (N1)
{
var s = "";
var n2 = 0;
for (var i = 0; i < 4; i++)
{
N2 = Math.pow (2,3-i);
if (N1 >= n2)
{
s + + ' 1 ';
N1 = N1-N2;
}
Else
s + + ' 0 ';

}
return s;

}
function Dig2dec (s)
{
var Retv = 0;
if (s.length = 4)
{
for (var i = 0; i < 4; i + +)
{
Retv + = eval (S.charat (i)) * MATH.POW (2, 3-i);
}
return RETV;
}
return-1;
}
function Hex2utf8 (s)
{
var RetS = "";
var TempS = "";
var ss = "";
if (s.length = 16)
{
TempS = "1110" + s.substring (0, 4);
TempS + = "Ten" + s.substring (4, 10);
TempS + = "Ten" + s.substring (10,16);
var sss = "0123456789ABCDEF";
for (var i = 0; i < 3; i + +)
{
RetS + = "%";
SS = temps.substring (I * 8, (eval (i) +1) *8);



RetS + + Sss.charat (Dig2dec (ss.substring (0,4)));
RetS + + Sss.charat (Dig2dec (ss.substring (4,8)));
}
return RetS;
}
Return "";
}

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.