(65)CRC32校验C语言版本

  1. #include<iostream>
  2. # include <stdio.h>
  3. # include <string.h>
  4. typedef unsigned int uint ;
  5. uint POLYNOMIAL = 0xEDB88320 ;
  6. int have_table = 0 ;
  7. uint table[256] ;
  8. void make_table()
  9. {
  10. int i, j, crc ;
  11. have_table = 1 ;
  12. for (i = 0 ; i < 256 ; i++)
  13. for (j = 0, table[i] = i ; j < 8 ; j++)
  14. table[i] = (table[i]>>1)^((table[i]&1)?POLYNOMIAL:0) ;
  15. }
  16. uint crc32(uint crc, char *buff, int len)
  17. {
  18. if (!have_table) make_table() ;
  19. crc = ~crc;
  20. for (int i = 0; i < len; i++)
  21. crc = (crc >> 8) ^ table[(crc ^ buff[i]) & 0xff];
  22. return ~crc;
  23. }
  24. int main ()
  25. {
  26. char s[] = "676547657567";
  27. printf("%08Xh ", crc32(0, s, strlen(s)));
  28. system("pause");
  29. return 0 ;
  30. }
原文地址:https://www.cnblogs.com/wycBlog/p/7659716.html