gpt4 book ai didi

c# - 在 NodeJS 中模拟 C# 溢出

转载 作者:IT老高 更新时间:2023-10-28 23:26:54 24 4
gpt4 key购买 nike

我正在尝试将 C# 代码转换为 nodejs,但我碰壁了。 C# 中的一个函数使用一个字节使用 BitConverter.toInt64 生成 3 个数字,如下所示:

    var hashText = //Generates Hash from an input here using ComputeHash

var hashCodeStart = BitConverter.ToInt64(hashText, 0);
var hashCodeMedium = BitConverter.ToInt64(hashText, 8);
var hashCodeEnd = BitConverter.ToInt64(hashText, 24);


//Does other stuff with the three pieces here

例如,如果我使用数组:

var hash = new Byte[] {0xAA, 0x9B, 0x50, 0xA7, 0x56, 0x8D, 0x2A, 0x99, 0x87, 0xA7, 0x24, 0x10, 0xF8,0x1E, 0xC3, 0xA2, 0xF9, 0x57, 0x1A, 0x2D, 0x69, 0x89, 0x83, 0x91, 0x2D, 0xFA, 0xA5, 0x4A, 0x4E, 0xA2, 0x81, 0x25};

那么 start、middle 和 end 的值分别是:

Start : -7409954833570948182

Middle: -6718492168335087737

End : 2702619708542548525

但是使用带有 biguinut-format package 的 NodeJS我得到以下数字(代码如下):

start : 12293508287479753369

middle : 9774821171531793314

end : 17966858020764353425

使用以下 NodeJS

    var hexed = "aa9b50a7568d2a9987a72410f81ec3a2f9571a2d698983912dfaa54a4ea28125"
var format = require('biguint-format')

console.log(hexed.toUpperCase().slice(0, 16))
console.log("Start is " + format(hexed.toUpperCase().slice(0, 16), 'dec'))

console.log(hexed.toUpperCase().slice(16, 32))
console.log("Middle is " + format(hexed.toUpperCase().slice(16, 32), 'dec'))

console.log(hexed.toUpperCase().slice(32, 48))
console.log("End is " + format(hexed.toUpperCase().slice(32, 48), 'dec'))

我知道由于一些溢出,C# 的数字为负数,但问题是溢出似乎发生在 int64 可以存储的最大值之前。

我有没有办法找出这是哪个数字,或者我有什么其他方法可以模拟 C# 代码?

最佳答案

您使用字符串而不是缓冲区并拆分咬合,因此您有一个 16 位数字而不是 8 个十六进制值的数组

var buffer1 = new Buffer([0xAA, 0x9B, 0x50, 0xA7, 0x56, 0x8D, 0x2A, 0x99, 0x87]);
format(buffer1, 'dec', {format:'LE'})

你可能仍然会得到一个 uint,所以你需要在之后将它转换为一个有符号的 int

正如@argaz 提到的,BitConverter 通常是小端,因此需要 LE 标志。

关于c# - 在 NodeJS 中模拟 C# 溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38794301/

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