简体   繁体   English

CRC32作为.NET HashAlgorithm和字节序

[英]CRC32 as .NET HashAlgorithm and endianness

CRC32 is calculated as uint32 , while HashAlgorithm in .NET by convention returns byte[] . CRC32计算为uint32 ,而.NET中的HashAlgorithm按照惯例返回byte[] I can, of course, easily convert it with bytes = BitConverter.GetBytes(hash) but this is affected by "endianness" of a system (almost no chance for big-endian, of course). 我当然可以用bytes = BitConverter.GetBytes(hash)轻松地进行转换,但这受系统“字节序”的影响(当然,对于大字节序几乎没有机会)。

Anyway, I've been thinking is there any convention to follow? 无论如何,我一直在想要遵循什么约定? I have a feeling that it should be big-endian to make hash.ToString("X") and bytes.ToHex() (assuming .ToHex() exists) look the same. 我有一种感觉,使hash.ToString("X")bytes.ToHex() (假设存在.ToHex() )应该是相同的。

I've checked https://github.com/vurdalakov/crc32/wiki/CRC32 and it does not do that. 我已经检查了https://github.com/vurdalakov/crc32/wiki/CRC32 ,它没有做到这一点。 Any thoughts? 有什么想法吗?

I can only cite examples, where the zip and gzip file formats store the CRC in little-endian order. 我只能举出一些示例,其中zip和gzip文件格式以小端顺序存储CRC。 I'm sure someone can find examples where a 32-bit CRC is stored in big-endian order, sometimes called "network order" as big-endian is intended to be a convention for network communications. 我敢肯定有人会发现一些示例,其中32位CRC以大端顺序存储,有时也称为“网络顺序”,因为大端顺序是网络通信的约定。

If you are defining your own protocol, then you can pick whichever you like. 如果要定义自己的协议,则可以选择任意一个。 For the code to be portable, you would need to use shift operators to pick apart the bytes so that the endianess of the machine does not affect the result. 为了使代码具有可移植性,您需要使用移位运算符将字节分开,以便机器的字节序不会影响结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM