Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert javascript unicode notation code to utf-8?

Is there a command line tool or online service that can convert javascript unicode notation code to utf-8?

E.g. I got this json code, but it is hard to edit in common text editor.

{"name":"leon zhang","city":"\u4e0a\u6d77"}

I want to convert it to:

{"name":"leon zhang","city":"上海"}
like image 295
Leon Avatar asked May 14 '12 02:05

Leon


People also ask

How do I change Unicode to UTF-8?

Click Tools, then select Web options. Go to the Encoding tab. In the dropdown for Save this document as: choose Unicode (UTF-8). Click Ok.

Are JavaScript strings UTF-8?

While a JavaScript source file can have any kind of encoding, JavaScript will then convert it internally to UTF-16 before executing it. JavaScript strings are all UTF-16 sequences, as the ECMAScript standard says: When a String contains actual textual data, each element is considered to be a single UTF-16 code unit.

Does JavaScript use UTF-8 or UTF-16?

Most JavaScript engines use UTF-16 encoding, so let's detail into UTF-16. UTF-16 (the long name: 16-bit Unicode Transformation Format) is a variable-length encoding: Code points from BMP are encoded using a single code unit of 16-bit. Code points from astral planes are encoded using two code units of 16-bit each.

What is Unicode in JavaScript?

Unicode in Javascript source code In Javascript, the identifiers and string literals can be expressed in Unicode via a Unicode escape sequence. The general syntax is \uXXXX , where X denotes four hexadecimal digits. For example, the letter o is denoted as '\u006F' in Unicode.


1 Answers

You use the below javascript function to convert unicode to utf-8

function encode_utf8( s ){
    return unescape( encodeURIComponent( s ) );
}( '\u4e0a\u6d77' )

You can also try it out in firebug console. It works.

like image 176
premnathcs Avatar answered Oct 22 '22 14:10

premnathcs