How to resolve the algorithm CRC-32 step by step in the Visual Basic .NET programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm CRC-32 step by step in the Visual Basic .NET programming language

Table of Contents

Problem Statement

Demonstrate a method of deriving the Cyclic Redundancy Check from within the language.

The result should be in accordance with ISO 3309, ITU-T V.42, Gzip and PNG. Algorithms are described on Computation of CRC in Wikipedia. This variant of CRC-32 uses LSB-first order, sets the initial CRC to FFFFFFFF16, and complements the final CRC. For the purpose of this task, generate a CRC-32 checksum for the ASCII encoded string:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm CRC-32 step by step in the Visual Basic .NET programming language

Source code in the visual programming language

Public Class Crc32

    ' Table for pre-calculated values.
    Shared table(255) As UInteger

    ' Initialize table
    Shared Sub New()
        For i As UInteger = 0 To table.Length - 1
            Dim te As UInteger = i ' table entry
            For j As Integer = 0 To 7
                If (te And 1) = 1 Then te = (te >> 1) Xor &HEDB88320UI Else te >>= 1
            Next
            table(i) = te
        Next
    End Sub

    ' Return checksum calculation for Byte Array,
    '  optionally resuming (used when breaking a large file into read-buffer-sized blocks).
    ' Call with Init = False to continue calculation.
    Public Shared Function cs(BA As Byte(), Optional Init As Boolean = True) As UInteger
        Static crc As UInteger
        If Init Then crc = UInteger.MaxValue
        For Each b In BA
            crc = (crc >> 8) Xor table((crc And &HFF) Xor b)
        Next
        Return Not crc
    End Function

End Class


    ' Returns a Byte Array from a string of ASCII characters.
    Function Str2BA(Str As String) As Byte()
        Return System.Text.Encoding.ASCII.GetBytes(Str)
    End Function

    ' Returns a Hex string from an UInteger, formatted to a number of digits,
    '  adding leading zeros If necessary.
    Function HexF(Value As UInteger, Digits As Integer) As String
        HexF = Hex(Value)
        If Len(HexF) < Digits Then HexF = StrDup(Digits - Len(HexF), "0") & HexF
    End Function

    ' Tests Crc32 class
    Sub Test()
        Dim Str As String = "The quick brown fox jumps over the lazy dog"
        Debug.Print("Input = """ & Str & """")
        ' Convert string to Byte Array, compute crc32, and display formatted result
        Debug.Print("Crc32 = " & HexF(Crc32.cs(Str2BA(Str)), 8))
        ' This next code demonstrates continuing a crc32 calculation when breaking the input
        ' into pieces, such as processing a large file by a series of buffer reads.
        Crc32.cs(Str2BA(Mid(Str, 1, 20)))
        Debug.Print("Crc32 = " & HexF(Crc32.cs(Str2BA(Mid(Str, 21)), False), 8))
    End Sub


Input = "The quick brown fox jumps over the lazy dog"
Crc32 = 414FA339
Crc32 = 414FA339


  

You may also check:How to resolve the algorithm Equilibrium index step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Python programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the PHP programming language
You may also check:How to resolve the algorithm Arrays step by step in the OxygenBasic programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the PicoLisp programming language