从Hex值数组求和以计算校验和不起作用

时间:2016-03-24 02:48:26

标签: c# hex checksum

我有一个包含以下十六进制值的数组:

  

A8,00,00,11,01,44,33,28,0C,15,2B,00,28,FF,00,00,00,FE,FF,80,7F,00,00,FF ,00,00,00,FE,FF,80,7F,00,00,FF,00,00,00,FE,FF,80,7F,00,00,FF,00,00,00,FE,FF ,80,7F,00,00,FF,00,00,00,FE,FF,80,7F,00,00,FF,00,00,00,FE,FF,80,7F,00,00,FF ,00,00,00,FE,FF,80,7F,00,00,FF,00,00,00,FE,FF,80,7F,00,00,FF,00,00,00,FE,FF ,80,7F,00,00,FF,00,00,00,FE,FF,80,7F,00,00,FF,00,00,00,FE,FF,80,7F,00,00,FF ,00,00,00,FE,FF,80,7F,00,00,FF,00,00,00,FE,FF,80,7F,00,00,FF,00,00,00,FE,FF ,80,7F,00,00,FF,00,00,00,FE,FF,80,7F,00,00,0C,00,03,35,00,00,00

我需要从他们那里刷新校验和,我知道的结果是C5

我得到了以下内容,我只是看到它们含糊不清,因为它们含糊不清:

  • 校验和是通过对值进行求和得出的,结果也是“在十六进制中使用最右边的最大字节”

  • 所有数据总和的最低字节

  • 添加8位宽的数据

如果你更喜欢这个十进制:

  

168,0,0,    17,18,61,40,12,21,43,0,40,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,   255,0,0,0,254,255,128,127,0,0,    12,0,3,53,0,0,0

和十进制checksum is 197

任何帮助都将不胜感激。

编辑:

在回答RJM的问题时,我基本上尝试循环遍历值并总计LSB以及MSB。如下:

int LSB = 0; 
int MSB = 0;
foreach (Byte value in byteArray) {
    LSB += (value & 1);
    MSB += (value >> 7);
}
//Expected Checksum is 197
//LSB == 52
//MSB == 60

1 个答案:

答案 0 :(得分:0)

值得庆幸的是,我提供了更多信息,我可以发布解决方案。

int chk = 0;
foreach (Byte value in byteArray) {
    chk += value;
}
chk = (chk & 255); 

所以,基本上我需要对所有字节求和,然后使用结果的LSB。