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);