求将下边C++的CRC校验代码转换成java格式的?

面包你大爷 2013-03-01 03:25:29
void y_crc(unsigned char *buff, unsigned short bufflen, unsigned char *lsb, unsigned char *msb)
{
        unsigned short crc = 0xFFFF;          // Preload to FFFF
        unsigned short tempresults;           // Just a temporary results holder
        unsigned short bitindex;           // 0 - 7
        unsigned short byteindex;           // The byte pointer into the byte array that holds
        // the command to be checked.
        unsigned char  placeholder;           // A place to put the byte while we work on it.
 
        for (byteindex = 0; byteindex < bufflen ; byteindex++) // begin checking after SOH and before CRC bytes
        {
                placeholder = *(buff + byteindex);
                for(bitindex = 0; bitindex <= 7; bitindex++)
                {
                        tempresults = (crc >> 15) ^ (placeholder >> 7);    // Shift CRC right 15 bits then do a bitwise XOR
                        crc <<= 1;             // Shift CRC left one bit and store it in CRC
                        if(tempresults)
                        {
                                crc ^= 0x1021;           // Standard CCITT Polynomial X16+X12+X5+1
                        }
                        placeholder <<= 1;
                }
        }
        *lsb = crc&0x00ff;       
        *msb = (crc>>8)&0x00ff;       
}
...全文
201 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
面包你大爷 2013-06-13
  • 打赏
  • 举报
回复
楼上两位说的不无道理。问题我已解决,没用java中的CRC32类,而是拿c++的代码修改为java的
suciver 2013-03-04
  • 打赏
  • 举报
回复
引用 1 楼 shine333 的回复:
java.util.zip.CRC32
+1,java已经封装好了CRC32类
shine333 2013-03-03
  • 打赏
  • 举报
回复
java.util.zip.CRC32

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧