I have a unsigned char array of which I'd like to calculate the CRC32 checksum.
The CRC32 function also expects a unsigned char pointer, however, it interprets the array as an ASCII array.
This is the CRC function:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
unsigned int crc32(unsigned char *message)
{
int i, j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0) {
byte = message[i]; // Get next byte.
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
int main(int argc, char **argv)
{
unsigned char *arr;
if ((arr = malloc(64)) == NULL) {
perror("Could not allocate memory");
exit(EXIT_FAILURE);
}
char str[] = "47d46d17e759a1dec810758c08004510002127d90000401152e4c0a8b21fc0a8b2255b9b5b9c000db20caabbccddee00000000000000000000000000";
memcpy(arr, str, strlen(str));
// ...
unsigned int crc = crc32(arr);
printf("CRC: 0x%x\n", crc); // 0xB6BA014A instead of 0xBF6B57A2
return 0;
}
Now, I'd like to calculate the CRC32 but the unsigned char array has to be interpreted as an hex array.
F.ex., this is the result of the calculated CRC:
Input:
"47d46d17e759a1dec810758c08004510002127d90000401152e4c0a8b21fc0a8b2255b9b5b9c000db20caabbccddee00000000000000000000000000"
- as ASCII: 0xB6BA014A (this is what I usually get because it's interpreted as ASCII)
- as Hex.: 0xBF6B57A2 (this is the checksum I want)