How to resolve the algorithm CRC-32 step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm CRC-32 step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

' version 18-03-2017
' compile with: fbc -s console

Function crc32(buf As String) As UInteger<32>

    Static As UInteger<32> table(256)
    Static As UInteger<32> have_table
    Dim As UInteger<32> crc, k
    Dim As ULong i, j

    If have_table = 0 Then
        For i = 0 To 255
            k = i
            For j = 0 To 7
                If (k And 1) Then
                    k Shr= 1
                    k Xor= &Hedb88320
                Else
                    k Shr= 1
                End If
                table(i) = k
            Next
        Next
        have_table = 1
    End If

    crc = Not crc ' crc = &Hffffffff
    
    For i = 0 To Len(buf) -1
        crc = (crc Shr 8) Xor table((crc And &hff) Xor buf[i])
    Next

    Return Not crc

End Function

' ------=< MAIN >=------

Dim As String l = "The quick brown fox jumps over the lazy dog"
Dim As UInteger<32> crc

Print "input = "; l
print
Print "The CRC-32 checksum = "; Hex(crc32(l), 8)

' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

  

You may also check:How to resolve the algorithm Wilson primes of order n step by step in the Nim programming language
You may also check:How to resolve the algorithm Anonymous recursion step by step in the Clojure programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Ol programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the Wart programming language