Conversion between strings and Unicode encoding in JavaScript

Source: Internet
Author: User

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

 

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.