I was trying to implement my own CRC32 function in C#. I saw an elegant solution in JS here JavaScript CRC32 So I came up with this:
internal static class Crc32
{
internal static long CalculateCrc32(string str)
{
long[] crcTable = Crc32.MakeCrcTable();
long crc = 0 ^ (-1);
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
crc = (crc >> 8) ^ crcTable[(crc ^ c) & 0xFF];
}
return ~crc; //(crc ^ (-1)) >> 0;
}
internal static long[] MakeCrcTable()
{
long c;
long[] crcTable = new long[256];
for (int n = 0; n < 256; n++)
{
c = n;
for (int k = 0; k < 8; k++)
{
var res = c & 1;
c = (res == 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
}
crcTable[n] = c;
}
return crcTable;
}
}
The problem is that my solution does not return the same result. Console.WriteLine(Crc32.CalculateCrc32("l"));
results in 1762050814 while the JS function produces 2517025534. The JS result is also the correct one. What am I doing wrong?