【问题标题】:Compute CRC32 of a Byte Array计算字节数组的 CRC32
【发布时间】:2019-12-04 16:27:36
【问题描述】:

在 C# 中,我正在努力生成/计算 CRC32。目前我正在使用这种方法: (crc32类是从this source导入的)

Crc32 c = new Crc32();
var hash = string.Empty;
foreach(byte b in c.ComputeHash(package))
{
    hash += b.ToString("0x").ToLower();
}

然后:

Console.WriteLine(hash);

结果是:0048284b

但我想以 HEX 形式获取它。就像“0xD754C033”一样,我可以把它放在一个字节数组中。 对字节数组的 CRC32 计算进行了一些挖掘,但找不到任何可行的方法。

长话短说:如何计算字节数组的 CRC32 十六进制? (正确)

P.S:根据提供的链接,发布的答案不重复。

【问题讨论】:

  • 你为什么不用微软的 CNG Api
  • byte[] to hex string的可能重复
  • c.ComputeHash(package) 看起来像是返回一个字节数组。如果你想“把它放在一个字节数组中”,你已经有了一个......c.ComputeHash(package)的返回值
  • 完全不清楚您在这里要求什么,您提供的答案只会让事情更加混乱。

标签: c# hash checksum crc


【解决方案1】:

我找到了一个非常适合我的问题的解决方案。万一它发生在任何人身上,我会发布我目前解决问题的方法。也许不是最好的,但出于测试目的,它可以完成这项工作。

byte[] package= {255,13,45,56};
//The byte array that will be used to calculate CRC32C hex

foreach (byte b in package)
{
Console.WriteLine("0x{0:X}", b);
//Writing the elements in hex format to provide visibility (for testing)
//Take note that "0x{0:X}" part is used for hex formatting of a string. X can be changed depending on the context eg. x2, x8 etc.
}
Console.WriteLine("-");//Divider
/* Actual solution part */                
String crc = String.Format("0x{0:X}", Crc32CAlgorithm.Compute(package));
/* Actual solution part */ 
Console.WriteLine(crc);
/* Output: 0x6627634B */
/*Using CRC32.NET NuGet package for Crc32CAlgorithm class. 
Then calling Compute method statically and passing byte array to the method. Keep in mind it returns uint type value. Then writing the returned variable in hex format as described earlier. */

提到的 Nuget 包:Crc32.NET

【讨论】:

  • 什么是inner?它没有在您的代码 sn-p 中的任何地方定义。
  • 我的错。更新。感谢您的通知。
猜你喜欢
  • 2020-04-03
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 2010-12-23
  • 1970-01-01
  • 2020-03-27
  • 2010-12-03
  • 2013-10-21
相关资源
最近更新 更多