嗨,我正在将 c# 应用程序转换为 android,并从字节数组计算校验和,与 c# 中的相同。但它在字节数组下方返回错误值。任何人都请帮忙。谢谢。
C#字节数组:
[41、132、41、132、41、132、41、132、41、132、41、132、41、132、41、132、41、132、41、132、41、132、41、132、41 , 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132 , 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41 , 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132 , 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41 , 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132 , 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41 , 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41,132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41, 132, 41、132、41、132、41、132、41、132、41、132、41、132、41、132]
c#代码:
public static uint CalculateChecksum(byte[] buffer, int offset, int length)
{
uint cs = 0;
for (int i = offset; i < offset + length & i < buffer.Length; i += 2)
{
ushort s = BitConverter.ToUInt16(buffer, i);
cs += s;
}
return cs;
}
值得到 4736620 将此值转换为字节数组给出 [108,70,72,0]
安卓字节数组
[41、-124、41、-124、41、-124、41、-124、41、-124、41、-124、41、-124、41、-124、41、-124、41、-124 , 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124 , 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124 , 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124 , 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124 , 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124 , 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124 , 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124 , 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124,41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41、-124、41、-124、41、-124、41、-124]41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124]41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124, 41, -124]
安卓代码:
public static long checkSum(byte[] buffer, int offset, int length) {
long cs = 0;
for (int i = offset; i < offset + length & i < buffer.length; i += 2) {
ByteBuffer bb = ByteBuffer.allocate(2);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(buffer[i]);
bb.put(buffer[i+1]);
long shortVal = bb.getShort(0);
cs += shortVal;
}
return cs;
}
值得到 -4438420 将此值转换为字节数组
ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt((int) value).array();
给出 [108,70,-68,-1]
做错了什么?为什么它返回不同的值?请帮我。谢谢你。