Noa*_*oah 43 javascript unicode
我不熟悉Javascript,并且正在寻找返回字符的UNICODE值的函数,并且给定UNICODE值,返回等效的字符串.我确信有一些简单的东西,但我没有看到它.
例:
pax*_*blo 69
看一下:
String.fromCharCode(64)
Run Code Online (Sandbox Code Playgroud)
和
String.charCodeAt(0)
Run Code Online (Sandbox Code Playgroud)
第一个必须在String 类(字面意思String.fromCharCode...)上调用,并返回"@"(对于64).第二个应该在String 实例上运行(例如"@@@".charCodeAt...),并返回第一个字符的Unicode代码('0'是字符串中的位置,您可以通过将其更改为另一个字符来获取字符串中其他字符的代码数).
脚本片段:
document.write("Unicode for character ? is: " + "?".charCodeAt(0) + "<br />");
document.write("Character 2580 is " + String.fromCharCode(2580) + "<br />");
Run Code Online (Sandbox Code Playgroud)
得到:
Unicode for character ? is: 2580 Character 2580 is ?
由于JavaScript内部使用UCS-2,String.fromCharCode(codePoint)因此不适用于补充Unicode字符。例如,如果codePoint为119558(0x1D306,则为''字符)。
如果您要基于非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'
Run Code Online (Sandbox Code Playgroud)
如果要获取字符串中每个字符的Unicode代码点,则需要将UCS-2字符串转换为UTF-16代码点的数组(每个替代对形成一个代码点)。您可以为此使用Punycode.js的实用程序功能:
punycode.ucs2.decode('abc'); // [97, 98, 99]
punycode.ucs2.decode(''); // [119558]
Run Code Online (Sandbox Code Playgroud)