Tomislav Markovski
Tomislav Markovski

Reputation: 12346

How do I convert this checksum code from C# to Java?

I need help converting this code from C# to Java.

    public static ulong GetChecksum(byte[] data)
    {
        ulong sum = 0;
        foreach (var item in data)
            sum += item;

        return ulong.MaxValue - sum + 1;
    }

The Java version would return long, instead ulong.

Edit: I haven't tried anything, because I don't know how to handle the last line ulong.MaxValue. Basically, what I need for the end result is to produce the same number that is (long)GetChecksum(buffer) in C# to be the same as getChecksum(buffer) in java. I'm very new to java, which is why I ask this.

Edit2: Here is the final solution. Many helpful comments. Thank you guys.

public static byte[] getChecksum(byte[] data) {
        BigInteger sum = BigInteger.ZERO;
        for (byte s : data)
            sum = sum.add(BigInteger.valueOf((long) ((char) s & 0xFF)));

        return new BigInteger("FFFFFFFFFFFFFFFF", 16).subtract(sum).add(BigInteger.ONE).toByteArray();
    }

Edit2: Too bad this question got downrated. It has many useful comments.

Upvotes: 0

Views: 486

Answers (2)

vcsjones
vcsjones

Reputation: 141598

ulong doesn't have an equivalent in Java. However, you can work around it with BigInteger.

private static BigInteger Checksum(short[] data)
{
    BigInteger sum = BigInteger.ZERO;  
    for (short s : data)  
        sum = sum.add(BigInteger.valueOf((long)s)); 

    return new BigInteger("18446744073709551615", 10).subtract(sum).add(BigInteger.ONE); 
}

Also, since bytes are signed in Java, you have to go up to short to get 0-255.

EDIT: If your signature needs to match using byte[] instead of short[], then billc.cn has the answer.

Upvotes: 1

billc.cn
billc.cn

Reputation: 7317

Since Java does not throw errors on arithmetic overflow, you should be able to do something like:

long sum = 0;
for (byte b : data)
    sum += (long) b;

return sum;

The return was simplified based on the assumption that you want the exact same result (in binary rather than in value) as in C#. ulong.MaxValue is 64 bits of 1, which is -1 in signed long which cancels the "+1".

Edit: I told a lie here. The result should be logically -sum, which due to the default two's compliment implementation, can't be easily done. You should consider the BigInteger solution instead.

Edit2: Because signed subtraction behaves non-uniformly, the unsigned operation ulong.MaxValue - sum can't be simulated trivially. What I suggest you do is:

long sum = 0;
for (byte b : data)
    sum += ((long) b) & 0xFFL; // To prevent sign-expansion. Sorry, I missed this the first time as well.

return (new BigInteger("FFFFFFFFFFFFFFFF", 16)).subtract((new BigInteger(Long.toHexString(sum), 16))).add(BigInteger.ONE);

This looks ugly but I tried my best to use native arithmetic as much as possible.

You can use BitInteger's "toByteArray()" method to get the raw binary for comparison. Do not use longValue() as it destroys the first bit.

Upvotes: 1

Related Questions