gpt4 book ai didi

c++ - 这种 CRC32 方法的大端兼容版本会是什么样子?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:13:07 29 4
gpt4 key购买 nike

我正在开展一个项目,该项目需要对正在传输的数据进行 CRC32 检查。我想让我的代码不仅兼容 Intel 架构(“Little Endian”),而且兼容 Solaris 架构(“Big Endian”)。我发现这个“CCRC32”在两台小端机器上工作得很好,但完全没有通过任何跨平台测试:

代码:

CCRC32.h & CCRC32.cpp(取自维基百科的“外部链接”)

http://en.wikipedia.org/wiki/Cyclic_redundancy_check

这是代码的方法示例:

void CCRC32::PartialCRC(unsigned long *ulCRC, const unsigned char *sData, unsigned long ulDataLength) {
while(ulDataLength--) {
//If your compiler complains about the following line, try changing each
//occurrence of *ulCRC with "((unsigned long)*ulCRC)" or "*(unsigned long *)ulCRC".

*(unsigned long *)ulCRC =
((*(unsigned long *)ulCRC) >> 8)
^ this->ulTable[((*(unsigned long *)ulCRC) & 0xFF) ^ *sData++];
}




unsigned long CCRC32::FullCRC(const unsigned char *sData, unsigned long ulDataLength) {
unsigned long ulCRC = 0xffffffff; //Initilaize the CRC.
this->PartialCRC(&ulCRC, sData, ulDataLength);
return(ulCRC ^ 0xffffffff); //Finalize the CRC and return.
}

所以我的问题是:你们中的大端专家是否知道如何调整上述方法以使用大端机器,或者是否有人知道可以实现我的目标的现有源代码?到目前为止,我的搜索一直没有成功。

谢谢你的时间,

詹姆斯

最佳答案

不确定是否有帮助,但是 this piece of C code有大端和小端版本。

关于c++ - 这种 CRC32 方法的大端兼容版本会是什么样子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5827163/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com