CRC-32: Difference between revisions

→‎{{header|Go}}: added implementation
(→‎{{header|Ruby}}: algorithm is not correct, however description might not)
(→‎{{header|Go}}: added implementation)
Line 21:
 
=={{header|Go}}==
===Library===
<lang go>package main
 
Line 35 ⟶ 36:
output
<pre>414FA339</pre>
===Implementation===
<lang go>package main
 
import "fmt"
 
var table [256]uint32
 
func init() {
for i := range table {
word := uint32(i)
for j := 0; j < 8; j++ {
if word&1 == 1 {
word = (word >> 1) ^ 0xedb88320
} else {
word >>= 1
}
}
table[i] = word
}
}
 
func crc32(s string) uint32 {
crc := ^uint32(0)
for i := 0; i < len(s); i++ {
crc = table[byte(crc)^s[i]] ^ (crc >> 8)
}
return ^crc
}
 
func main() {
fmt.Printf("%0x\n", crc32("The quick brown fox jumps over the lazy dog"))
}</lang>
Output:
<pre>
414fa339
</pre>
 
=={{header|J}}==
1,707

edits