I can't seem to figure out and duplicate a checksum calculation. Perhaps it's no surprise as I'm not really familiar with bitwise operations.
Could someone please look at the following and advise? It would most appreciated.
Where am I going wrong?
Could someone please look at the following and advise? It would most appreciated.
Here's the C code I'm trying to duplicate:
static uint32_t createCheckSum(char * fName)
{
int fp = fopen(fName, “R”);
uint32_t checkSum = 0;
char c;
while (fgets(c, 1, fp))
{
checkSum ^= c;
checkSum <<= 1;
if (checkSum & 0x80000000)
{
checkSum |= 1;
}
}
fclose(fp);
return checkSum;
}
And here's my faulty attempt in B4A:
Sub CalculateChecksum As Long
Dim checkSum = 0 As Long
Dim MyBytes() As Byte = File.ReadBytes(Starter.FilesDir, "MyData.bin" )
For i = 0 To MyBytes.Length - 1
checkSum = Bit.XorLong(checkSum,MyBytes(i))
checkSum = Bit.ShiftLeftLong(checkSum,1)
If Bit.AndLong(checkSum,0x80000000) = 0x80000000 Then
checkSum = Bit.OrLong(checkSum,0x01)
End If
Next
Return checkSum
End Sub
Where am I going wrong?