Javascript:将(十六进制)有符号整数转换为 javascript 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13468474/
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
Javascript: convert a (hex) signed integer to a javascript value
提问by mythbu
I have a signed value given as a hex number, by example 0xffeb
and want convert it into -21
as a "normal" Javascript integer.
例如,我有一个以十六进制数给出的有符号值,0xffeb
并希望将其转换-21
为“正常”Javascript 整数。
I have written some code so far:
到目前为止,我已经编写了一些代码:
function toBinary(a) { //: String
var r = '';
var binCounter = 0;
while (a > 0) {
r = a%2 + r;
a = Math.floor(a/2);
}
return r;
}
function twoscompl(a) { //: int
var l = toBinaryFill(a).length;
var msb = a >>> (l-1);
if (msb == 0) {
return a;
}
a = a-1;
var str = toBinary(a);
var nstr = '';
for (var i = 0; i < str.length; i++) {
nstr += str.charAt(i) == '1' ? '0' : '1';
}
return (-1)*parseInt(nstr);
}
The problem is, that my function returns 1 as MSB for both numbers because only at the MSB of the binary representation "string" is looked. And for this case both numbers are 1:
问题是,我的函数对于两个数字都返回 1 作为 MSB,因为只在二进制表示形式“字符串”的 MSB 处查看。对于这种情况,两个数字都是 1:
-21 => 0xffeb => 1111 1111 1110 1011
21 => 0x15 => 1 0101
Have you any idea to implement this more efficient and nicer?
你有什么想法来实现这个更有效和更好的吗?
Greetings, mythbu
问候,神话
回答by Bart Friederichs
Use parseInt()
to convert (which just accepts your hex string):
使用parseInt()
转换(这只是接受你的十六进制字符串):
parseInt(a);
Then use a mask to figure out if the MSB is set:
然后使用掩码确定是否设置了 MSB:
a & 0x8000
If that returns a nonzero value, you know it is negative.
如果它返回一个非零值,你就知道它是负数。
To wrap it all up:
总结一下:
a = "0xffeb";
a = parseInt(a, 16);
if ((a & 0x8000) > 0) {
a = a - 0x10000;
}
Note that this only works for 16-bit integers (short
in C). If you have a 32-bit integer, you'll need a different mask and subtraction.
请注意,这仅适用于 16 位整数(short
在 C 中)。如果您有一个 32 位整数,则需要不同的掩码和减法。
回答by milosmns
I came up with this
我想出了这个
function hexToInt(hex) {
if (hex.length % 2 != 0) {
hex = "0" + hex;
}
var num = parseInt(hex, 16);
var maxVal = Math.pow(2, hex.length / 2 * 8);
if (num > maxVal / 2 - 1) {
num = num - maxVal
}
return num;
}
And usage:
和用法:
var res = hexToInt("FF"); // -1
res = hexToInt("A"); // same as "0A", 10
res = hexToInt("FFF"); // same as "0FFF", 4095
res = hexToInt("FFFF"); // -1
So basically the hex conversion range depends on hex's length, ant this is what I was looking for. Hope it helps.
所以基本上十六进制转换范围取决于十六进制的长度,这就是我要找的。希望能帮助到你。
回答by Kostynha
Based on @Bart FriederichsI've come with:
基于@ Bart Friederichs我来了:
function HexToSignedInt(num, numSize) {
var val = {
mask: 0x8 * Math.pow(16, numSize-1), // 0x8000 if numSize = 4
sub: -0x1 * Math.pow(16, numSize) //-0x10000 if numSize = 4
}
if(parseInt(num, 16) & val.mask > 0) { //negative
return (val.sub + parseInt(num, 16))
}else { //positive
return (parseInt(num,16))
}
}
so now you can specify the exact length (in nibbles).
所以现在您可以指定确切的长度(以半字节为单位)。
var numberToConvert = "CB8";
HexToSignedInt(numberToConvert, 3);
//expected output: -840
回答by Kitani Islam
function hexToSignedInt(hex) {
if (hex.length % 2 != 0) {
hex = "0" + hex;
}
var num = parseInt(hex, 16);
var maxVal = Math.pow(2, hex.length / 2 * 8);
if (num > maxVal / 2 - 1) {
num = num - maxVal
}
return num;
}
function hexToUnsignedInt(hex){
return parseInt(hex,16);
}
the first for signed integer and the second for unsigned integer
第一个用于有符号整数,第二个用于无符号整数