I am examining a .d2s file using HxD, which lets me see the unencoded bytes of the file.
The file is about 900+ bytes.
Bytes 12 to 15 are a checksum.
The algorithm for calculating the checksum is as follows:
sum = (sum << 1) + data[i];
And here is the github page where I got that formula.
https://github.com/krisives/d2s-format#checksum
A lot of other websites report that this is the correct formula for this type of file, so I am confident it is right. But I am having a hard time understanding and implementing it so that I get a sensible result.
<<
is a binary shift to the left, and if I do (sum << 1)
, I'm shifting the current checksum (in binary) to the left by 1 position (or multiplying the decimal equivalent by 2) before I add the next byte.
But I have 900 bytes in the file. I'm getting a HUGE number (way bigger than something that will fit inside 4 bytes or 32 bits or 2^32), which is correct given my understanding. So I think I am wrong about this.
Here is a fiddle where I try to calculate the checksum:
I just copy the contents of the file and paste them into the textarea and the checksum gets output to the page and the console.
This whole exercise is for learning purposes.