0

嗨,我正在将 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]

做错了什么?为什么它返回不同的值?请帮我。谢谢你。

4

1 回答 1

0

字节数组的内容不同(例如,C# 中的第二个位置与 Android 中的不同)。所以从我的角度来看,代码返回不同的校验和是可以的。

编辑:数组的内容确实不同。一个由有符号字节组成,另一个由无符号字节组成。这是一个差异(即使位表示可能相同)并且与大端与小端无关。您面临的问题是因为在 Java 版本中,您再次使用签名短裤,而 C# 版本使用未签名短裤。在那里,符号使计算错误的项是 2^16 的倍数。事实上,如果您在 C# 中使用 signed int16,您也会得到与 Android 解决方案相同的结果。

因此,简单的解决方案是从字节缓冲区请求一个整数,而不是一个短整数,因为后者是有符号的。或者,您可以添加 2^16 以防 short 为负数(ushort 永远不可能):

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);
        if (shortVal < 0) { shortVal += (1 << 16); }
        cs += shortVal;
    }
    return cs;
}
于 2016-10-26T11:53:55.383 回答