Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Checksum


' This is the official polynomial used by CRC32 in PKZip.
' Often the polynomial is shown reversed (04C11DB7).
Dim dwPolynomial As Long
dwPolynomial = &HEDB88320
Dim i As Integer , j As Integer
Redim crc32Table(256)
Dim dwCrc As Long

For i = 0 To 255
   dwCrc = i
    For j = 8 To 1 Step - 1
        If (dwCrc And 1) Then
           dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
           dwCrc = dwCrc Xor dwPolynomial
        Else
           dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
        End If
    Next j
   crc32Table(i) = dwCrc
Next i

'open a file and get a Byte array
Open "c:\test.txt" For Binary As #1
    Redim X(LOF(1)) As Byte
    Get #1, , X
Close #1

'calculate the CRC value itself
Dim crc32Result As Long
Dim iLookup As Integer
crc32Result = &HFFFFFFFF

For i = 0 To Ubound (X)
     iLookup = (crc32Result And &HFF) Xor X(i)
     crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And 16777215   ' nasty shr 8 with vb :/
     crc32Result = crc32Result Xor crc32Table(iLookup)
Next

crc32 = Not (crc32Result)

MsgBox crc32