Convert unicode string javascript

String.fromCharCode()

The String.fromCharCode() static method returns a string created from the specified sequence of UTF-16 code units.

Try it

Syntax

.fromCharCode(num1) String.fromCharCode(num1, num2) String.fromCharCode(num1, num2, /* …, */ numN) 

Parameters

A number between 0 and 65535 ( 0xFFFF ) representing a UTF-16 code unit. Numbers greater than 0xFFFF are truncated to the last 16 bits. No validity checks are performed.

Return value

A string of length N consisting of the N specified UTF-16 code units.

Description

Because fromCharCode() is a static method of String , you always use it as String.fromCharCode() , rather than as a method of a String value you created.

Unicode code points range from 0 to 1114111 ( 0x10FFFF ). charCodeAt() always returns a value that is less than 65536 , because the higher code points are represented by a pair of 16-bit surrogate pseudo-characters. Therefore, in order to produce a full character with value greater than 65535 , it is necessary to provide two code units (as if manipulating a string with two characters). For information on Unicode, see UTF-16 characters, Unicode code points, and grapheme clusters.

Because fromCharCode() only works with 16-bit values (same as the \u escape sequence), a surrogate pair is required in order to return a supplementary character. For example, both String.fromCharCode(0xd83c, 0xdf03) and «\ud83c\udf03» return code point U+1F303 «Night with Stars». While there is a mathematical relationship between the supplementary code point value (e.g. 0x1f303 ) and both surrogate values that represent it (e.g., 0xd83c and 0xdf03 ), it does require an extra step to either calculate or look up the surrogate pair values every time a supplementary code point is to be used. For this reason, it’s more convenient to use String.fromCodePoint() , which allows for returning supplementary characters based on their actual code point value. For example, String.fromCodePoint(0x1f303) returns code point U+1F303 «Night with Stars».

Читайте также:  Маркеры тире в html

Examples

Using fromCharCode()

BMP characters, in UTF-16, use a single code unit:

.fromCharCode(65, 66, 67); // returns "ABC" String.fromCharCode(0x2014); // returns "—" String.fromCharCode(0x12014); // also returns "—"; the digit 1 is truncated and ignored String.fromCharCode(8212); // also returns "—"; 8212 is the decimal form of 0x2014 

Supplementary characters, in UTF-16, require two code units (i.e. a surrogate pair):

.fromCharCode(0xd83c, 0xdf03); // Code Point U+1F303 "Night with String.fromCharCode(55356, 57091); // Stars" === "\uD83C\uDF03" String.fromCharCode(0xd834, 0xdf06, 0x61, 0xd834, 0xdf07); // "\uD834\uDF06a\uD834\uDF07" 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jul 3, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

JavaScript String.fromCharCode()

The String.fromCharCode() method converts Unicode values to characters.

The String.fromCharCode() is a static method of the String object.

The syntax is always String.fromCharCode() .

You cannot use myString.fromCharCode() .

Syntax

Parameters

Return Value

Tip

For a list of all Unicode values, please study our Complete Unicode Reference.

Browser Support

String.fromCharCode() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Читайте также:  Python decode bytes online

Источник

Оцените статью