计算校验和返回android

时间:2016-10-26 11:48:29

标签: java c# android bitconverter

您正在将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,13 2,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#c​​ode:

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]

Android字节数组

[41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41 ,-1 24,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41, - 124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41, - 124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41, - 124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41,-124,41, - 124]

Android代码:

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]

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

1 个答案:

答案 0 :(得分:0)

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

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

因此,简单的解决方案是从字节缓冲区请求一个整数,而不是短整数,因为后者是有符号的。或者,你可以添加2 ^ 16,以防短路为负(这是一个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;
}