[英]javascript Convert string representation of hex value to hex
In Javascript, how do I convert a string representation of a hex value into it's hex representation ?在 Javascript 中,如何将十六进制值的字符串表示形式转换为它的十六进制表示形式?
What I have returning from a checksum routine is a string value "FE".我从校验和例程返回的是一个字符串值“FE”。 What I need is it's hex representation "\\xFE"我需要的是它的十六进制表示 "\\xFE"
I cannot simply do this, as it gives me an error:我不能简单地这样做,因为它给了我一个错误:
var crc = "FE";
var hex = "\x" + crc;
This just gives me a new 4 character ASCII string:这只是给了我一个新的 4 个字符的 ASCII 字符串:
var crc = "FE";
var hex = "0x" + "FE";
thxs for any guidance.谢谢任何指导。
像这样
var hex = parseInt("FF", 16);
For the string \\xFE
, escape the backslash: var hex = '\\\\x'+'FE'
对于字符串\\xFE
,转义反斜杠: var hex = '\\\\x'+'FE'
To convert 'FE'
to a Number use +('0xFE')
要将'FE'
转换为数字,请使用+('0xFE')
To show +('0xFE')
as a hexadecimal, use (224).toString(16)
, or '0x'+((254).toString(16))
要将+('0xFE')
为十六进制,请使用(224).toString(16)
或'0x'+((254).toString(16))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.