echo Impossible|sed 's/Im/To be /'

December 05, 2010

Tip. zlib crc32 function

zlib crc32 function howto


crc32 常用來作為驗証通訊資料,zlib 也提供了這個功能,簡單方便好用。


crc.c howto

#include <zlib.h>
#include <stdio.h>

int main () {
  unsigned long crc;

  crc = crc32 (0L, Z_NULL, 0);
  crc = crc32 (crc, (unsigned char *) "lloyd\n", 6);
  printf ("%x\n", (unsigned int) crc);
  crc = crc32 (0L, Z_NULL, 0);
  crc = crc32 (crc, (unsigned char *) "lloyd huang\n", 12);
  printf ("%x\n", (unsigned int) crc);
  return (0);
}

Makefile

gcc -o crc crc.c -lz -g -Wall

驗証方法

$> ./crc
4b1707f9
359cde0d
$> echo lloyd > testfile; crc32 testfile
4b1707f9
$> echo "lloyd huang" > testfile; crc32 testfile
359cde0d