May 14, 2024, 06:34:35 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


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