gpt4 book ai didi

javascript - 在 JavaScript 中将字节数组转换为有符号 int64

转载 作者:行者123 更新时间:2023-11-28 00:34:39 27 4
gpt4 key购买 nike

我知道js中没有int64数据类型。但我需要将字节数组转换为有符号的 int64 以获得与 .NET 类库中的 BitConverter.ToInt64() 方法相同的结果。 javascript (BigInt.js) 中有一个大整数的实现,它接受十六进制作为输入并获取 bigInt 作为输出,但结果与 BCL 中的 BitConverter.ToInt64() 不同。示例:

BitConverter.ToInt64() for '0x40e00a35661f92a2' will result in -6732283969128439744

我需要一个在 java 脚本中执行此操作的解决方案,性能不是问题。

编辑:

按正常顺序将数据输入 BigInt.js:

javascript: new BigInt('0x40e00a35661f92a2')// shows 4674747637673464482

以相反的顺序将数据输入 BigInt.js:

javascript: new BigInt('0xa2921f66350ae040')// shows 11714460104581111872

两者都不匹配 BitConverter.ToInt64() 结果:

C SHARP: BitConverter.ToInt64(new byte[] { 0x40, 0xe0, 0x0a, 0x35, 0x66, 0x1f, 0x92, 0xa2 }, 0); // shows -6732283969128439744

最佳答案

这里有一个将十六进制字符串转换为 Int64(有符号和无符号)的实现:LINK

该代码返回正确的结果:

javascript: new HexStringToInt64StringConverter(true).convert('a2921f66350ae040') // will return -6732283969128439744 same as BitConverter.ToInt64() in BCL

链接中的功能:

function HexStringToInt64StringConverter(signed) {
var hexCode = {
'0':"0000",
'1':"0001",
'2':"0010",
'3':"0011",
'4':"0100",
'5':"0101",
'6':"0110",
'7':"0111",
'8':"1000",
'9':"1001",
'a':"1010",
'b':"1011",
'c':"1100",
'd':"1101",
'e':"1110",
'f':"1111"
};
var preComputedLongMath = {
"20":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
"21":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
"22":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4],
"23":[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0, 0, 0, 0, 2, 2, 5, 1, 7, 9, 9, 8, 1, 3, 6, 8, 5, 2, 4, 8],
"252":[0, 0, 0, 0, 4, 5, 0, 3, 5, 9, 9, 6, 2, 7, 3, 7, 0, 4, 9, 6],
"253":[0, 0, 0, 0, 9, 0, 0, 7, 1, 9, 9, 2, 5, 4, 7, 4, 0, 9, 9, 2],
"254":[0, 0, 0, 1, 8, 0, 1, 4, 3, 9, 8, 5, 0, 9, 4, 8, 1, 9, 8, 4],
"255":[0, 0, 0, 3, 6, 0, 2, 8, 7, 9, 7, 0, 1, 8, 9, 6, 3, 9, 6, 8],
"256":[0, 0, 0, 7, 2, 0, 5, 7, 5, 9, 4, 0, 3, 7, 9, 2, 7, 9, 3, 6],
"257":[0, 0, 1, 4, 4, 1, 1, 5, 1, 8, 8, 0, 7, 5, 8, 5, 5, 8, 7, 2],
"258":[0, 0, 2, 8, 8, 2, 3, 0, 3, 7, 6, 1, 5, 1, 7, 1, 1, 7, 4, 4],
"259":[0, 0, 5, 7, 6, 4, 6, 0, 7, 5, 2, 3, 0, 3, 4, 2, 3, 4, 8, 8],
"260":[0, 1, 1, 5, 2, 9, 2, 1, 5, 0, 4, 6, 0, 6, 8, 4, 6, 9, 7, 6],
"261":[0, 2, 3, 0, 5, 8, 4, 3, 0, 0, 9, 2, 1, 3, 6, 9, 3, 9, 5, 2],
"262":[0, 4, 6, 1, 1, 6, 8, 6, 0, 1, 8, 4, 2, 7, 3, 8, 7, 9, 0, 4],
"263":[0, 9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0, 8],
"264":[1, 8, 4, 4, 6, 7, 4, 4, 0, 7, 3, 7, 0, 9, 5, 5, 1, 6, 1, 6],
"265":[3, 6, 8, 9, 3, 4, 8, 8, 1, 4, 7, 4, 1, 9, 1, 0, 3, 2, 3, 2]
};
if (typeof(signed) != 'boolean') signed = false;
function toBinary(hex) {
hex = hex.toLowerCase();
var binary = "";
for (var i = 0; i < hex.length; i++) {
binary += hexCode[hex[i]];
}
return binary;
}

function to1nsComplement(binary) {
var compliment = "";
for (var i = 0; i < binary.length; i++) {
compliment += (binary.charAt(i) == "1" ? "0" : "1");
}
return compliment;
}

function arrayAdd(a, b) {
var carry = 0;
var number = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 19; i >= 0; i--) {
number[i] = a[i] + b[i] + carry;
if (number[i].toString().length > 1) {
carry = parseInt(number[i].toString().substring(0, number[i].toString().length - 1), 10);
number[i] = parseInt(number[i].toString().substring(number[i].toString().length - 1), 10)
} else {
carry = 0;
}
}
return number;
}

function removeZeroPad(number) {
var lock = false;
var output = [];
for (var i = 0; i < number.length; i++) {
if (lock) {
output.push(number[i]);
} else {
if (number[i] != 0) {
lock = true;
output.push(number[i]);
}
}
}
return output;
}

function binaryToDec(binary) {
var negative = false;
if (signed && (binary.charAt(0) == 1)) {
negative = true;
}
if (signed) {
binary = binary.substring(1);
if (negative) {
binary = to1nsComplement(binary);
}
}
var pos = 0;
var number = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (var i = binary.length - 1; i >= 0; i--) {
if (binary.charAt(i) == 1) {
number = arrayAdd(number, preComputedLongMath["2" + pos])
}
pos++;
}
if (negative) {
number = removeZeroPad(arrayAdd(number, preComputedLongMath["20"]));
number.splice(0, 0, "-");
} else {
number = removeZeroPad(number);
}
return number.join("");
}

this.convert = function (hex) {
var binary = toBinary(hex);
return binaryToDec(binary);
};
}

关于javascript - 在 JavaScript 中将字节数组转换为有符号 int64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28623673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com