javascript javascript中的整数到十六进制字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23122373/
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-28 00:23:01  来源:igfitidea点击:

Int to hex string in javascript

javascript

提问by Upvote

I want to convert an number (integer) to a hex string

我想将数字(整数)转换为十六进制字符串

2 (0x02) to "\x02"

or

或者

62 (0x0062) to "\x62"

How can I do that correctly?

我怎样才能正确地做到这一点?

回答by Ibu

You can use the to string method:

您可以使用 to string 方法:

a = 64;
a.toString(16); // prints "40" which is the hex value
a.toString(8); // prints "100" which is the octal value
a.toString(2); // prints "1000000" which is the binary value

回答by volter9

Well, it's seems that you want just to concatenate the integer with \x.

好吧,您似乎只想将整数与 \x 连接起来。

If so just to like that:

如果是这样只是为了喜欢:

var number = 62;
var hexStr = '\x' + number.toString(16);

But you have something strange about explaining.

但是你解释起来有些奇怪。

Note: that 62 is not the same as 0x62, 0x62 would be 98.

注意:62 与 0x62 不同,0x62 将是 98。

回答by Neel

var converted = "\x" + number.toString(16)

var converted = "\x" + number.toString(16)