I try to calculate the CRC32 of a file with a script ruby, but I does
not obtain the good result by comparison with many of the other software
which already make this (in other programming language...), for example: http://www.codeproject.com/KB/recipes/crc32.aspx
The result looks like well of an CRC32 but it is never good...
I also try a pure ruby implementation with this code but it makes
exactly the zlib.crc32 result... :
_____________________
def crc32(c)
n = c.length
r = 0xFFFFFFFF
n.times do |i|
r ^= c[i]
8.times do
if (r & 1)!=0
r = (r>>1) ^ 0xEDB88320
else
r >>= 1
end
end
end
r ^ 0xFFFFFFFF
end
File.open('bibi.jpg', 'rb') { |h| f = h.read } ; nil
=> nil
Zlib.crc32(f,0).to_s(16)
=> "5ba68c3c"
And the second CRC corresponds to what other tools give me.
Fred
···
Le 23 février 2009 à 10:44, Paul Golea a écrit :
--
You ask me if I've known love
And what it's like to sing songs in the rain Well, I've seen love come
And I've seen it shot down I've seen it die in vain
Shot down in a blaze of glory (Bon Jovi, Blaze of Glory)
to this: (mode of 'r', not 'rb')
f = nil
File.open('bibi.jpg', 'r') { |h| f = h.read } ; nil
And you'll get the same result. File.read is not the problem, but the use of text mode (which is the default). Under Unix, there is no difference between text mode and binary mode, but the line endings under Windows are \015\012.