RosettaCodeData/Task/CRC-32/FreeBASIC/crc-32.basic

51 lines
1.0 KiB
Plaintext

' 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