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

Discussion in 'Ruby' started by Ralph Smith, Oct 25, 2005.

  1. Ralph Smith

    Ralph Smith Guest

    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
     
    Ralph Smith, Oct 25, 2005
    #1
    1. Advertising

  2. 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.
     
    Yukihiro Matsumoto, Oct 25, 2005
    #2
    1. Advertising

  3. Ralph Smith

    Ara.T.Howard Guest

    Re: ruby gives different answer for checksum of files on windowsand FreeBSD?

    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
    ===============================================================================
     
    Ara.T.Howard, Oct 25, 2005
    #3
  4. "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
     
    Michael Campbell, Oct 25, 2005
    #4
  5. 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.
     
    Yukihiro Matsumoto, Oct 25, 2005
    #5
  6. Ralph Smith

    Pit Capitain Guest

    Re: ruby gives different answer for checksum of files on windowsand FreeBSD?

    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
     
    Pit Capitain, Oct 25, 2005
    #6
  7. Ralph Smith

    Pit Capitain Guest

    Re: ruby gives different answer for checksum of files on windowsand FreeBSD?

    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
     
    Pit Capitain, Oct 26, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Seberino
    Replies:
    3
    Views:
    1,213
    Christian Seberino
    Feb 5, 2004
  2. John fabiani
    Replies:
    14
    Views:
    521
    Donn Cave
    Jul 2, 2004
  3. Ola Natvig
    Replies:
    12
    Views:
    1,158
    Nick Craig-Wood
    Feb 11, 2005
  4. Max Williams
    Replies:
    4
    Views:
    220
    Max Williams
    Sep 18, 2009
  5. jiing
    Replies:
    6
    Views:
    389
    Michele Dondi
    Dec 2, 2004
Loading...

Share This Page