Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created March 17, 2015 08:16
Show Gist options
  • Save jsfaint/2acdf3dd19628ab18ee4 to your computer and use it in GitHub Desktop.
Save jsfaint/2acdf3dd19628ab18ee4 to your computer and use it in GitHub Desktop.
Calculate checksum
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
char *szFilename = 0;
FILE *fp;
unsigned long data;
unsigned long checksum = 0;
int size = 0;
if (argc == 1)
printf("need input file name\n");
szFilename = argv[1];
fp = fopen(szFilename, "rb");
while(fread(&data, 4, 1, fp))
{
size += 4;
checksum ^= data;
}
fclose(fp);
printf("size=%d, checksum=%08X\n", size, checksum);
fp = fopen(szFilename, "ab");
fwrite(&checksum, 4, 1, fp);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment