javascript 十六进制的字符表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7746098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Character representation from hexadecimal
提问by The Mask
Is it possible to convert a hexadecimal value to its respective ASCII character, not using the String.fromCharCode
method, in JavaScript?
是否可以String.fromCharCode
在 JavaScript 中不使用该方法将十六进制值转换为其各自的 ASCII 字符?
For example:
例如:
JavaScript:
JavaScript:
0x61 // 97
String.fromCharCode(0x61) // a
C-like:
C类:
(char)0x61 // a
回答by rid
You can use the \xNN
notation:
您可以使用\xNN
符号:
var str = "\x61";
回答by pimvdb
Not in that fashion, because JavaScript is loosely typed, and does not allow one to define a variable's data type.
不是那种方式,因为 JavaScript 是松散类型的,并且不允许定义变量的数据类型。
What you can do, though, is creating a shortcut:
但是,您可以做的是创建一个快捷方式:
var char = String.fromCharCode; // copy the function into another variable
Then you can call char
instead of String.fromCharCode
:
然后你可以调用char
而不是String.fromCharCode
:
char(0x61); // a
Which is quite close to what you want (and perhaps more readable/requiring less typing).
这与您想要的非常接近(并且可能更具可读性/需要更少的输入)。
回答by david
There is also the Unicode equivalent of \x
:
还有 Unicode 等价物\x
:
var char = "\u0061";