Conversion between strings and Unicode encoding in JavaScript
Conversion between strings and Unicode encoding in JavaScript
This Code demonstrates conversion between string and Unicode encoding in JavaScript:
// For the convenience of the console demonstration, the var definition is not added to the variable // avoid the // string str = Chinese in actual programming; // obtain the character char0 = str. charAt (0); // numerical code value code = str. charCodeAt (0); // 20013 // Code Conversion str0 = String. fromCharCode (code); // convert to a hexadecimal array code16 = code. toString (16); // 4e2d // convert to literal representation ustr = \ u + code16; // in the format of // packaged as JSONjsonstr = '{ustr: '+ ustr +'} '; //' {ustr: Medium} '// use the JSON tool to convert obj = JSON. parse (jsonstr); // Object {ustr: Medium} // ustr_n = obj. ustr; // Medium
If it is a group of strings, you need to useForLoop.
Here, we use the JSON tool for conversion.
If you want to be compatible with IE 6 and other browsers, You can parse them as follows:
If (object === typeof message) {// if it is an object, no conversion is made.} else if (window [JSON]) {message = JSON. parse (message);} else {// IE6, IE7 message = eval (+ message + ));}
The console debugging result is as follows:
Date: January 1, August 03, 2015