Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ruby gives different answer for checksum of files on windows and FreeBSD?

Reply
Thread Tools

ruby gives different answer for checksum of files on windows and FreeBSD?

 
 
Ralph Smith
Guest
Posts: n/a
 
      10-25-2005


If I run this code to compute a "checksum" of a file I get a different answer on a windows machine
and a FreeBSD machine. Does anyone know why? Or a better way to get a quick checksum of a file?

fname = ARGV[0]

size = File.size(fname);
checksum = 0;

f = File.new(fname)
f.each_byte {|x| checksum += x }

printf("%s: %d ( %x hex )\n", fname, checksum, checksum)


Thanks,
Ralph

 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      10-25-2005
Hi,

In message "Re: ruby gives different answer for checksum of files on windows and FreeBSD?"
on Tue, 25 Oct 2005 17:37:02 +0900, Ralph Smith <> writes:

|f = File.new(fname)

Try

f = File.new(fname, "rb")

and see how it works.


matz.


 
Reply With Quote
 
 
 
 
Ara.T.Howard
Guest
Posts: n/a
 
      10-25-2005
On Tue, 25 Oct 2005, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "Re: ruby gives different answer for checksum of files on windows and FreeBSD?"
> on Tue, 25 Oct 2005 17:37:02 +0900, Ralph Smith <> writes:
>
> |f = File.new(fname)
>
> Try
>
> f = File.new(fname, "rb")
>
> and see how it works.


this comes up so often - would it make sense to be the default mode on
windows?

-a
--
================================================== =============================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
================================================== =============================



 
Reply With Quote
 
Michael Campbell
Guest
Posts: n/a
 
      10-25-2005
"Ara.T.Howard" <> writes:

> On Tue, 25 Oct 2005, Yukihiro Matsumoto wrote:
>
> > Hi,
> >
> > In message "Re: ruby gives different answer for checksum of files on windows and FreeBSD?"
> > on Tue, 25 Oct 2005 17:37:02 +0900, Ralph Smith <> writes:
> >
> > |f = File.new(fname)
> >
> > Try
> >
> > f = File.new(fname, "rb")
> >
> > and see how it works.

>
> this comes up so often - would it make sense to be the default mode on
> windows?


I'm guessing here, but my guess is that reading text files with ruby
is more common than non-text files. Does "rb" affect that?

--
I tend to view "truly flexible" by another term: "Make everything
equally hard". -- DHH
 
Reply With Quote
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      10-25-2005
Hi,

In message "Re: ruby gives different answer for checksum of files on windows and FreeBSD?"
on Tue, 25 Oct 2005 23:21:21 +0900, "Ara.T.Howard" <> writes:

|> f = File.new(fname, "rb")
|>
|> and see how it works.
|
|this comes up so often - would it make sense to be the default mode on
|windows?

Unless your program communicates with other programs on Windows, that
use text-mode.

matz.


 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      10-25-2005
Yukihiro Matsumoto schrieb:
> Hi,
>
> In message "Re: ruby gives different answer for checksum of files on windows and FreeBSD?"
> on Tue, 25 Oct 2005 23:21:21 +0900, "Ara.T.Howard" <> writes:
>
> |> f = File.new(fname, "rb")
> |>
> |> and see how it works.
> |
> |this comes up so often - would it make sense to be the default mode on
> |windows?
>
> Unless your program communicates with other programs on Windows, that
> use text-mode.


Could someone provide an example for such a program? My Ruby programs
communicate with Vim and SQL*Plus, but I haven't used text-mode yet.

Regards,
Pit


 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      10-26-2005
Sean O'Halpin schrieb:
> On 10/25/05, daz <> wrote:
>
>>C's "fopen" seems to have missed this fact (??)

>
> It's part of ANSI C for stdio to open files by default in text mode.
> This does nothing on Unix but it converts CRLF (\r\n) to LF (\n) on
> Windows. This means you can write portable text processing programs in
> C (and in Ruby). If you're handling binary, you have to know what
> you're doing to write portable programs - binmode is the least of it.
>
> Changing to binmode by default would cause a lot more confusion for
> newbies and casual programmers on Windows. There'd be a lot of "how do
> I get rid of this \r character?" posts for a start. I reckon a lot
> more than binmode causes.


Thanks to you and Daz for the detailed answer. So it seems that the
editors you normally use make a difference whether you prefer text or
binary mode.

Regards,
Pit


 
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
Compare 2 files with different names (checksum?) Max Williams Ruby 4 09-18-2009 10:03 PM
Wrong answer equals to a blank answer or not? Zadkin Microsoft Certification 8 06-27-2006 01:51 PM
Wrong answer equals to a blank answer or not? Zadkin Microsoft Certification 0 06-23-2006 09:17 PM
Efficient checksum calculating on lagre files Ola Natvig Python 12 02-11-2005 05:30 PM
C extension=> pow(2,1) gives DIFFERENT answers in different parts of C extension!?!?! Any ideas why? Christian Seberino Python 3 02-05-2004 04:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57