将C语言的CRC32 代码转成JAVA的CRC32 代码
2016-01-05 11:35
399 查看
public class CustomerCRC32 { private static long[] crc32Table = new long[256]; static { long crcValue; for (int i = 0; i < 256; i++) { crcValue = i; for (int j = 0; j < 8; j++) { if ((crcValue & 1) == 1) { crcValue = crcValue >> 1; crcValue = 0x00000000edb88320L ^ crcValue; } else { crcValue = crcValue >> 1; } } crc32Table[i] = crcValue; } } public static long getCrc32(byte[] bytes) { long resultCrcValue = 0x00000000ffffffffL; for (int i = 0; i < bytes.length; i++) { int index = (int) ((resultCrcValue ^ bytes[i]) & 0xff); resultCrcValue = crc32Table[index] ^ (resultCrcValue >> 8); } resultCrcValue = resultCrcValue ^ 0x00000000ffffffffL; return resultCrcValue; } public static void main(String[] args) { String testStr = "{\"log\":{\"content\":\"2\",\"time\":\"2016-01-05 10:17:24\",\"type\":1001,\"version\":\"[5.0.8.12]\"},\"pcInfo\":{\"ip\":\"192.168.118.57\",\"mac\":\"94-DE-80-A8-E6-EC\",\"onlyId\":\"7CE81DDBF7D05F6AD89CD7D79FAA5905\"},\"user\":{\"name\":\"CFM\"}}"; java.util.zip.CRC32 jdkCrc32 = new java.util.zip.CRC32(); jdkCrc32.update(testStr.getBytes()); System.out.println("jdk crc32: " + jdkCrc32.getValue()); System.out.println("test crc32: " + getCrc32(testStr.getBytes())); } }
相关文章推荐
- Performanced C++ 经验规则(1):你不知道的构造函数(上)
- C++基础学习教程(七)----类编写及类的两个特性解析--->多态&继承
- c++碰到的几个问题
- [转]c++类的构造函数详解
- 【ThinkingInC++】52、函数内部的静态变量
- VS2010 C++环境下DLL和LIB文件目录及名称修改
- C++ 小知识点总结
- C++实现添加桌面右键新建菜单
- 位运算求平均值
- 用gperftools对C/C++程序进行profile
- C++库常用函数一览
- c++链表类的实现
- C++ 中的比較函数
- C语言笔记之结构体
- C++函数模板详解
- 螺旋矩阵(C语言实现)- 2016.01.04
- C++模板类声明与定义
- C语言数据库
- 笔试,面试,C/C++,判断单链表是否带环?若带环,求环长度,求环入口点(两种方法)
- 一个c++的开发人员,大学毕业时必须掌握哪些课程和技能?