如何将字符串转换为unicode字符?

时间:2021-06-29 20:19:21

In Javascript '\uXXXX' returns in a unicode character. But how can I get a unicode character when the XXXX part is a variable?

在Javascript'\ uXXXX'中返回一个unicode字符。但是当XXXX部分是变量时,如何获得unicode字符?

For example:

var input = '2122';
console.log('\\u' + input);             // returns a string: "\u2122"
console.log(new String('\\u' + input)); // returns a string: "\u2122"

The only way I can think of to make it work, is to use eval; yet I hope there's a better solution:

我能想到的唯一方法就是使用eval;但我希望有更好的解决方案:

var input = '2122';
var char = '\\u' + input;
console.log(eval("'" + char + "'"));    // returns a character: "™"

5 个解决方案

#1


25  

Use String.fromCharCode() like this: String.fromCharCode(parseInt(input,16)). When you put a Unicode value in a string using \u, it is interpreted as a hexdecimal value, so you need to specify the base (16) when using parseInt.

像这样使用String.fromCharCode():String.fromCharCode(parseInt(input,16))。使用\ u将Unicode值放入字符串时,它将被解释为十六进制值,因此在使用parseInt时需要指定base(16)。

#2


14  

String.fromCharCode("0x" + input)

String.fromCharCode(“0x”+输入)

or

String.fromCharCode(parseInt(input, 16)) as they are 16bit numbers (UTF-16)

String.fromCharCode(parseInt(input,16))因为它们是16位数字(UTF-16)

#3


9  

JavaScript uses UCS-2 internally.

JavaScript在内部使用UCS-2。

Thus, String.fromCharCode(codePoint) won’t work for supplementary Unicode characters. If codePoint is 119558 (0x1D306, for the '????' character), for example.

因此,String.fromCharCode(codePoint)不适用于补充Unicode字符。例如,如果codePoint是119558(0x1D306,对于'????'字符)。

If you want to create a string based on a non-BMP Unicode code point, you could use Punycode.js’s utility functions to convert between UCS-2 strings and UTF-16 code points:

如果要基于非BMP Unicode代码点创建字符串,可以使用Punycode.js的实用程序函数在UCS-2字符串和UTF-16代码点之间进行转换:

// `String.fromCharCode` replacement that doesn’t make you enter the surrogate halves separately
punycode.ucs2.encode([0x1d306]); // '????'
punycode.ucs2.encode([119558]); // '????'
punycode.ucs2.encode([97, 98, 99]); // 'abc'

#4


1  

var hex = '2122';
var char = unescape('%u' + hex);

console.log(char);

will returns " ™ "

将返回“™”

#5


0  

Since ES5 you can use

由于ES5你可以使用

String.fromCodePoint(number)

to get unicode values bigger than 0xFFFF.

获取大于0xFFFF的unicode值。

So, in every new browser, you can write it in this way:

因此,在每个新浏览器中,您都可以这样编写它:

var input = '2122';
console.log(String.fromCodePoint(input));

or if it is a hex number:

或者如果是十六进制数:

var input = '2122';
console.log(String.fromCodePoint(parseInt(input, 16)));

More info:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint

#1


25  

Use String.fromCharCode() like this: String.fromCharCode(parseInt(input,16)). When you put a Unicode value in a string using \u, it is interpreted as a hexdecimal value, so you need to specify the base (16) when using parseInt.

像这样使用String.fromCharCode():String.fromCharCode(parseInt(input,16))。使用\ u将Unicode值放入字符串时,它将被解释为十六进制值,因此在使用parseInt时需要指定base(16)。

#2


14  

String.fromCharCode("0x" + input)

String.fromCharCode(“0x”+输入)

or

String.fromCharCode(parseInt(input, 16)) as they are 16bit numbers (UTF-16)

String.fromCharCode(parseInt(input,16))因为它们是16位数字(UTF-16)

#3


9  

JavaScript uses UCS-2 internally.

JavaScript在内部使用UCS-2。

Thus, String.fromCharCode(codePoint) won’t work for supplementary Unicode characters. If codePoint is 119558 (0x1D306, for the '????' character), for example.

因此,String.fromCharCode(codePoint)不适用于补充Unicode字符。例如,如果codePoint是119558(0x1D306,对于'????'字符)。

If you want to create a string based on a non-BMP Unicode code point, you could use Punycode.js’s utility functions to convert between UCS-2 strings and UTF-16 code points:

如果要基于非BMP Unicode代码点创建字符串,可以使用Punycode.js的实用程序函数在UCS-2字符串和UTF-16代码点之间进行转换:

// `String.fromCharCode` replacement that doesn’t make you enter the surrogate halves separately
punycode.ucs2.encode([0x1d306]); // '????'
punycode.ucs2.encode([119558]); // '????'
punycode.ucs2.encode([97, 98, 99]); // 'abc'

#4


1  

var hex = '2122';
var char = unescape('%u' + hex);

console.log(char);

will returns " ™ "

将返回“™”

#5


0  

Since ES5 you can use

由于ES5你可以使用

String.fromCodePoint(number)

to get unicode values bigger than 0xFFFF.

获取大于0xFFFF的unicode值。

So, in every new browser, you can write it in this way:

因此,在每个新浏览器中,您都可以这样编写它:

var input = '2122';
console.log(String.fromCodePoint(input));

or if it is a hex number:

或者如果是十六进制数:

var input = '2122';
console.log(String.fromCodePoint(parseInt(input, 16)));

More info:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint