JS converts string encoding to Unicode encoding

Source: Internet
Author: User
Tags eval

Javascript has built-in conversion functions: String.fromcharcodeat (), string.charcodeat () for converting between strings and encodings.

So what we're going to do with the string is actually very simple, we can get the Unicode encoding of the string directly using the String.charcodeat () function, where the number is decimal, and we number.tostring () the method, The 16-in-binary Unicode encoding of the string can be active.

By assembling, we can get a standard Unicode form of string, as described in the following ways:

The code is as follows Copy Code

function Tounicode (str) {
var temp,
i = 0,
R = ',
len = str.length;

for (; i < Len; i++) {
temp = Str.charcodeat (i). toString (16);

while (Temp.length < 4)
temp = ' 0 ' + temp;

R + + ' \\u ' + temp;
};

return R;
}


So how do we convert the Unicode code to Chinese?

The following methods are commonly used for converting such data to Chinese problems.

  code is as follows copy code

1. Eval resolution or New Function ("' + str + '") ()

str = eval ("" + str + ""); "I am Unicode Encoding"

2. unescape resolves

str = unescape (str.replace (/\u/g, "%u"));//"I am Unicode Encoding"

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.