[REBOL] CRC16 poly 8005
From: gschwarz::netconnect::com::au at: 24-Oct-2006 13:41
After a bit of playing around I come up with this function to do a CRC.
Any feedback welcome.
Regards,
Greg
data: "123456789"
crc-8005: func ["returns a 4 byte string crc of string of data" data][
crc: 0
foreach char data [
char: to-integer to-binary char
loop 8 [
either ( to-integer crc and 32768 ) <> 0 [ temp: 32773 crc: ( crc and
32767)]
[ temp: 0 ]
crc: (( crc * 2 ) + ( char and 1 )) xor to-integer temp
char: to-integer char / 2
]
crc: crc and 65535 ; keep crc in the 16 bit range
]
; equiv to adding two zero bytes to end of string
loop 16 [
either ( to-integer crc and 32768 ) <> 0 [ temp: 32773 ][ temp: 0 ]
crc: ( crc * 2 ) xor to-integer temp
crc: crc and 65535 ; keep crc in the 16 bit range
]
; Reflect crc
ref: 0
loop 16 [
ref: to-integer ( ref * 2 )
ref: ref + ( crc and 1 )
crc: to-integer crc / 2
]
crc: remove/part to-string to-hex ref 4
return crc
]
print crc-8005 data
print "should be BB3D"