Jump to content

CRC-32: Difference between revisions

1,517 bytes added ,  12 years ago
(→‎{{header|Go}}: added implementation)
Line 72:
414fa339
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>link hexcvt,printf
 
procedure main()
s := "The quick brown fox jumps over the lazy dog"
a := "414FA339"
printf("crc(%i)=%s - implementation is %s\n",
s,r := crc32(s),if r == a then "correct" else "in error")
end
 
procedure crc32(s) #: return crc-32 (ISO 3309, ITU-T V.42, Gzip, PNG) of s
static crcL,mask
initial {
crcL := list(256) # crc table
p := [0,1,2,4,5,7,8,10,11,12,16,22,23,26] # polynomial terms
mask := 2^32-1 # word size mask
every (poly := 0) := ior(poly,ishift(1,31-p[1 to *p]))
every c := n := 0 to *crcL-1 do { # table
every 1 to 8 do
c := iand(mask,
if iand(c,1) = 1 then
ixor(poly,ishift(c,-1))
else
ishift(c,-1)
)
crcL[n+1] := c
}
}
crc := ixor(0,mask) # quick implementation
every crc := iand(ixor(crcL[iand(255,ixor(crc,ord(!s)))+1],ishift(crc,-8)),mask)
return hexstring(ixor(crc,mask))
end</lang>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/hexcvt.icn hexcvt.icnprovides hex and hexstring]
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icnprovides formatting]
 
Output:<pre>crc("The quick brown fox jumps over the lazy dog")=414FA339 - implementation is correct</pre>
 
=={{header|J}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.