此 CRC32 方法的大端兼容版本会是什么样子?

发布于 2024-11-03 20:14:10 字数 1340 浏览 7 评论 0原文

我正在开发一个项目,需要对正在传输的数据进行 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.
}

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

谢谢你的宝贵时间,

詹姆斯

I'm working on a project that requires a CRC32 check to be done on data that is being transmitted. I would like to make my code compatible for not only Intel architecture ("Little Endian"), but for Solaris architecture ("Big Endian") as well. I've found this "CCRC32" that works spiffy for two little endian machines, but utterly fails any cross platform tests:

Code:

CCRC32.h & CCRC32.cpp
(taken off of wikipedia's "external links")

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

Here is a method sample of the code:

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.
}

So my question is this: Do you any of you big endian gurus know how to tweak the above methods to work with big endian machines, or does anyone know of an existing piece of source code that could accomplish my goal? I've been unsuccessful in my search thus far.

Thank you for your time,

James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-11-10 20:14:10

不确定它是否有帮助,但是这段C代码有大小端版本。

Not sure if it helps, but this piece of C code has big and small endian versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文