将c代码转换为js代码后 由于c中定义的变量为 uint 整型8位类型 运行发现由于JS中没有无法自动转换为整型 出现负数时候后会导致计算失败
我该如何改写js代码,还是将负数转换位整数后再计算 底层算法没算明白 求教导
js右偏移分为 有符号右移**(>>)** 和无符号右移**(>>>)** 两种 而c中只有一种
public static unsafe void crc64(ref Crc64 crc, void* data, uint len)
{
uint crc0 = crc.crc0;
uint crc1 = crc.crc1;
byte* cdata = (byte*)data;
while (len-- > 0)
{
int tab_index = ((int)(crc1 >> 24) ^ *cdata++) & 0xFF;
var para1 = crc1 << 8;
var para0 = crc0 >> 24;
crc1 = crc_table1[tab_index] ^ ((crc1 << 8) | (crc0 >> 24));
crc0 = crc_table0[tab_index] ^ (crc0 << 8);
}
crc.crc0 = crc0;
crc.crc1 = crc1;
}
//JS代码
crc64 function(buffer) {
crc64.crc0 = 0xffffffff;
crc64.crc1 = 0xffffffff;
var crc0 = crc64.crc0;
var crc1 = crc64.crc1;
var length = buffer.byteLength;
for (var i = 0; i < length; i++) {
var tab_index = ((crc1 >>> 24) ^ (buffer[i])) & 0xFF;
crc1 = crc_table1[tab_index] ^ ((crc1 << 8) | (crc0 >>> 24));
crc0 = crc_table0[tab_index] ^ (crc0 << 8);
console.log("tab_index:"+ tab_index + " "+"crc1:"+ crc1 + " "+"crc0:"+ crc0 + " " );
}
crc64.crc0 = crc0;
crc64.crc1 = crc1;
crc64.crc0 ^= 0xffffffff;
crc64.crc1 ^= 0xffffffff;
return crc64;
};
c运行结果
tab_index:255 crc1:1694702041 crc0:830097927
tab_index:101 crc1:514174325 crc0:374208445
tab_index:30 crc1:2636408049 crc0:2617598120
tab_index:157 crc1:755378055 crc0:1402886864
js运行结果 有负数
tab_index:255 crc1:1694702041 crc0:830097927
crc64.js:383 tab_index:101 crc1:514174325 crc0:374208445
crc64.js:383 tab_index:30 crc1:-1658559247 crc0:-1677369176
crc64.js:383 tab_index:157 crc1:755378055 crc0:1402886864
crc64.js:383 tab_index:45 crc1:1408777608 crc0:78960684