JS Unicode Encoding Conversion program

Source: Internet
Author: User
Tags html encode

JS Unicode Encoding Conversion program

/*
*js Unicode Encoding Conversion
*/
var dectohex = function (str) {
var res=[];
for (Var i=0;i < str.length;i++)
res[i]= ("+str.charcodeat" (i). toString). Slice (-4);
Return "\u" +res.join ("\u");
}
var decToHex2 = function (str) {
var res= "", C;
for (Var i=0;i < str.length;i++) {
C=str.charcodeat (i);
res+= (c< | | c==38 | | c>127)? " \u "+c.tostring": Str.charat (i)
}
return res
}
var hextodec = function (str) {
Str=str.replace (/\/g, "%");
return unescape (str);
}
var str=dectohex2 ("Dectohex Unicode Encoding Conversion");
Alert ("Encoded after:" +str+ "nn decoded:" +hextodec (str));
7.js HTML Encoding Conversion
/*
*js HTML Encode
*/
var htmlencode=function (str) {//html des encode.
var res=[];
for (Var i=0;i < str.length;i++)
Res[i]=str.charcodeat (i);
Return "&#" +res.join ("; &#") + ";";
};
var htmlencode2=function (s) {
var r = "", C;
for (var i = 0; i < s.length; i++) {
c = s.charcodeat (i);
R + + (C < | | c = = | | c > 127)? ("&#" + C + ";") : S.charat (i);
}
return R;
};
S.replace (/[u4e00-u9fa5]+)/g,function ($,$1) {
Return HtmlEncode ($);
//})
var htmlhexencode=function (str) {//html hex encode.
var res=[];
for (Var i=0;i < str.length;i++)
Res[i]=str.charcodeat (i). toString (16);
Return "&#" +string.fromcharcode (0x78) +res.join ("; &#" +string.fromcharcode (0x78)) + ";";/ /x, prevent FF & #x escaping
};
var htmldecode = function (str) {
Return Str.replace (/&# (x)? [^&] {1,5});? /g,function ($,$1,$2) {
Return String.fromCharCode ($, $16:10) (parseint);
});
};
var s= "HtmlEncode HTML encoding Conversion &#";
var S1=htmlencode (s) + "NN only pairs of double-byte and & encodings:" +htmlencode2 (s);
var s2=htmldecode (S1);
Alert ("Before coding:" +s+ "NN encoded:" +s1+ "nn decoded:" +s2);

var s= "Htmlhexencode HTML encoding Conversion &#";
var s1=htmlhexencode (s);
var s2=htmldecode (S1);
Alert ("Before coding:" +s+ "NN encoded:" +s1+ "nn decoded:" +s2);

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.