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.
Since Java does not throw errors on arithmetic overflow, you should be able to do something like:
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.MaxValueis 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 - sumcan’t be simulated trivially. What I suggest you do is: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.