Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET How do I caculate the CRC of a group of bytes (For example 04 01 00 47) using the CalculateCrc32 method?

Viewing 1 post (of 1 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #20588 |

    The following example demonstrates how to calcuate the CRC for a group if bytes:

    Dim data() As Byte = {&H4, &H1, &H0, &H47}

    ‘ When we wish to calculate a CRC32 in multiple calls to
    ‘ CalculateCrc32, we simply need to provide the
    ‘ “up to now” crc32 as the fourth parameter.
    ‘ On the first call, we pass zero.
    Dim crc32 As Long
    crc32 = ChecksumStream.CalculateCrc32(data, 0, data.Length, 0)

    ‘ To fold a crc32 to a crc16, we XOR the two halves
    ‘ of the crc32. In VB.NET, there is no “shift” operator.
    ‘ But everytime we shift one bit to the right, we divide
    ‘ by two. So shifting 16 bits to the right is the same
    ‘ as dividing by 2^16 (65536).
    Dim crc16 As Integer
    crc16 = (crc32 \ 65536) Xor (crc32 And &HFFFF)

    Console.WriteLine(“0x{0:X8} -> 0x{1:X4}”, crc32, crc16)

    Imported from legacy forums. Posted by Xceed admin (had 1680 views)

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.