This article illustrates the implementation of the conversion of string and Unicode encoding to each other in JavaScript. Share to everyone for your reference, specific as follows:
This code demonstrates the conversion of strings and Unicode encodings in javascript:
In order to facilitate the console demonstration, the variable does not add var definition//The
actual programming please avoid
//string
str = "Chinese";
Gets the character
char0 = Str.charat (0);//"Medium"/
digital encoded value
code = str.charcodeat (0);//20013//transcoding
str0 = Strin G.fromcharcode (code); "Medium"
///Convert to 16 array
code16 = code.tostring;//"4e2d"
///translate literal notation
USTR = "\\u" +CODE16;//"\u4e2d"
//packaging for JSON
jsonstr = ' {' USTR ': ' + ustr + ' '} ';//' {' ustr ': ' \u4e2d '} '
//Convert obj using JSON tool
= Json.parse (JSONSTR); Object {ustr: "Medium"}
//
ustr_n = obj.ustr;//"Medium"
If it is a set of strings, it needs to be handled using a for loop.
Where we use the JSON tool to convert.
If you want to be compatible with browsers such as IE6, you can resolve them in the following form:
if ("object" = = = typeof message) {
//If it is an object, do not convert
} else if (window["JSON"]) {A/
json.parse ( message);
} else {//IE6, IE7 message
= eval ("+ message +");
}
The console debugging results are as follows:
I hope this article will help you with JavaScript programming.