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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:09:22  来源:igfitidea点击:

Character representation from hexadecimal

javascriptcstringhexascii

提问by The Mask

Is it possible to convert a hexadecimal value to its respective ASCII character, not using the String.fromCharCodemethod, 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 \xNNnotation:

您可以使用\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 charinstead 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";