0

I'm trying to find a CRC32 computation algorythm that output data in positive 8-character HEX (like winrar CRC for example).

All algorythms found return a positive/negative integer that I don't know how to handle...

Teejay
  • 7,210
  • 10
  • 45
  • 76
  • Possible duplicate of [This question](http://stackoverflow.com/questions/8128/how-do-i-calculate-crc32-of-a-string) which links to [This answer](http://damieng.com/blog/2006/08/08/Calculating_CRC32_in_C_and_NET) – Matt Wilko Jun 27 '12 at 13:26
  • Already checked it out, but it returns unsigned int32. Can i convert it to HEX using .ToString("x16") ? – Teejay Jun 27 '12 at 13:30
  • http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net – Hiren Visavadiya Jun 27 '12 at 13:27

1 Answers1

6

I'll bet that all of the CRC32 algorithms you found return a 32-bit integer (e.g. int or uint). When you view it, you're probably viewing it as a base-10 number. By viewing I mean formatting the integer as a string without passing any format options to Int32.ToString or String.Format.

If you were to put Visual Studio into Hexadecimal View, you would get your "expected" output. The algorithms are all correct in this case, however, it is your expectation that is incorrect!

Instead, use a Number Format which produces the string representation you desire:

uint crc32 = Crc32Class.GetMeAValue(data);

// For example, we'll write base-10 and base-16 the output to the console
Console.WriteLine("dec: {0}", crc32);
Console.WriteLine("hex: {0:X8}", crc32);
user7116
  • 63,008
  • 17
  • 141
  • 172
  • Just let me understand: why X8 and not X16? – Teejay Jun 27 '12 at 13:37
  • 2
    @Teejay: there are 4-bits to a base-16 "digit" (hexit?) and 32-bits in a 32-bit integer, so: `(32 / 4) == 8`. – user7116 Jun 27 '12 at 13:40
  • Thank you one more time. So, assuming a Int16, I have to use "x4" ? – Teejay Jun 27 '12 at 14:56
  • Wouldn't it be easier to understand an argument that specifies the base? Something like tostring(16) – Teejay Jun 27 '12 at 15:37
  • No. If I have a Int32 as soruce value, the toString(16) would automatically do a toString("x8"), the toString(2) would automatically do a toString("x32"). Same way, if my source is a Int16, toString(16) would do a toString("x4") etc... – Teejay Jun 27 '12 at 15:53
  • Same broad range of outputs, lower complexity. Incidentally, a lot of programming languages support this way to operate base conversion. – Teejay Jun 27 '12 at 15:54
  • Actually "X" means convert to HEX, and 8 is the padding wanted. Please edit your comment to avoid confusion to other users – Teejay Jul 11 '12 at 15:52