Javascript:将hex编码的string转换为hex字节

现在我必须将string中编码的hex转换为hex字节。

var str = "5e" var b = // Should be 0x5e then. if str = "6b", then b = 0x6b and so on. 

有没有在JavaScript中的任何function,如在Java中

Byte.parseByte(str, 16)

提前致谢

你想要的function是parseInt

 parseInt("6b", 16) // returns 107 

parseInt的第一个参数是数字的string表示,第二个参数是基数。 十进制使用10,hex使用16。

从你的评论中,如果你期待string“6b”的“输出为0x6b”,那么只要在你的string前面加上“0x”,然后根据需要进一步操作。 没有Javascripttypes,它将以可读格式输出hex数字,除了string外,您将看到前缀为“0x”。

我刚刚解决了它

 new Buffer("32476832", 'hex') 

这解决了我的问题,并给了我所需的缓冲区

 <Buffer 32 47 68 32>