Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Cyclic redundancy check (CRC32) of a file

Reply
Thread Tools

Cyclic redundancy check (CRC32) of a file

 
 
Paul Golea
Guest
Posts: n/a
 
      02-23-2009
Hello,

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

I use this script :
_____________________

require 'zlib'
filetest = File.read 'image.jpg'
puts Zlib.crc32(filetest, 0).to_s(16).upcase
_____________________

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

filetest = File.read 'image.jpg'
puts crc32(filetest).to_s(16).upcase
_____________________


Thank you very much to the person who will have the solution of my
problem.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
F. Senault
Guest
Posts: n/a
 
      02-23-2009
Le 23 février 2009 à 10:44, Paul Golea a écrit :

> Thank you very much to the person who will have the solution of my
> problem.


If you are under Windows, you need to open the file in binary mode.
(Well, arguably, you always need to open binary files in binary mode).

For instance :

Z:\>ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Z:\>irb
>> require "zlib"

=> true

>> f = File.read('bibi.jpg') ; nil

=> nil
>> Zlib.crc32(f,0).to_s(16)

=> "332c7941"

>> f = nil

=> nil
>> 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
--
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)
 
Reply With Quote
 
 
 
 
Paul Golea
Guest
Posts: n/a
 
      02-23-2009
F. Senault wrote:
>
> And the second CRC corresponds to what other tools give me.
>
> Fred


Excellent!
I didn't know that File.read opened "badly" files under Windows...

Thank you very much.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      02-23-2009
On Feb 23, 2009, at 5:31 AM, Paul Golea wrote:

> F. Senault wrote:
>>
>> And the second CRC corresponds to what other tools give me.
>>
>> Fred

>
> Excellent!
> I didn't know that File.read opened "badly" files under Windows...
>
> Thank you very much.



Change this:
f = File.read('bibi.jpg') ; nil

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.

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cyclic redundancy check?? =?Utf-8?B?S1JJU1RJIE1BUkNP?= Microsoft Certification 2 05-07-2007 03:47 AM
how to program a CRC(cyclic redundancy check) calculation for java? subscribe Java 3 11-29-2006 02:07 PM
'cyclic redundancy check' UN DVD Video 1 12-16-2005 03:03 AM
Re: Connot Copy Webpad_Up: Data error(cyclic redundancy check) ???? °Mike° Computer Support 0 07-22-2003 09:47 PM
Re: Connot Copy Webpad_Up: Data error(cyclic redundancy check) ???? Brian H¹© Computer Support 0 07-22-2003 08:51 PM



Advertisments