[英]Converting unicode to hex
I have cyrillic word in my js file which charset is UTF-8 without BOM. 我的js文件中有西里尔字母,其字符集为UTF-8,而没有BOM。 How can I get hex index of unicode table of my first character?
如何获得我的第一个字符的unicode表的十六进制索引? The word "Абв" and result must return hex code "0x0410".
单词“Абв”和结果必须返回十六进制代码“ 0x0410”。
I tried this code, but it returns wrong result: 我尝试了这段代码,但是返回错误的结果:
var code = "А".charCodeAt(0);
var codeHex = code.toString(16).toUpperCase();
console.log(code, codeHex);
Note that in your console.log(code, codeHex);
注意,在您的
console.log(code, codeHex);
you have no space between the two values code
and codeHex
, so you'll get to see a seemingly big value ( 1040410
). 在两个值
code
和codeHex
之间没有空格,因此您将看到一个看似很大的值( 1040410
)。
So separate like this: 像这样分开:
console.log(code, ' ', codeHex);
and if you want a nice hex formatting, do this: 如果您想要一个很好的十六进制格式,请执行以下操作:
console.log(code, ' ', '0x' + ('0000' + codeHex).substr(-4));
Snippet: 片段:
var code = "А".charCodeAt(0); var codeHex = code.toString(16).toUpperCase(); document.write(code, ' ', '0x' + ('0000' + codeHex).substr(-4));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.