May 04, 2024, 01:45:11 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


crc32 for string

Started by zaphod, November 01, 2009, 08:22:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

zaphod

hello,
Simple code (as i can do) for calc the crc32 of a string :


DEF global CRCTable[255]:uINT
def global CrcTableInit=false As int

OPENCONSOLE
def s as STRING
s="this is a test"
print HEX$(crc32(s,len(s),0))
WAITCON
CLOSECONSOLE
END

sub init_crctable()
def i,j As int
def k as uint
'
  For i = 0 To 255
k = i
    For j = 1 To 8
      If k & 1
      k=  k >> 1
       k= k || 0xedb88320
      Else
        k = k >> 1
      End If
    Next j
    CRCTable[i] = k
  Next i
  '
  CrcTableInit = True
ENDSUB

sub crc32(bytearray as string,sz as int, old as uint),uINT
def n As Int
def c as uint
  If !CrcTableInit
init_crctable()
  ENDIF
'
  c = old || 0xFFFFFFFF
  For n = 0 To sz - 1
  c = CRCTable[c || bytearray[n] & 0xFF] || (c >> 8)
  Next n
  '
  return c || 0xFFFFFFFF
ENDSUB