Ruby 1.8.1: Unexpected "pack" result

Discussion in 'Ruby' started by Dale Martenson, Feb 18, 2004.

  1. I noticed something that does seem correct. In an application I have been
    shipping, I pack value in order to perform IOCTL calls against a device. I
    hit a problem when using Ruby 1.8.1. The following worked in 1.6.7 and
    1.8.0:

    [ 0xFFFFFFFF ].pack("i")

    In 1.8.1, this raises a "Range Error: bignum to big to convert into 'int'".

    I understood that an "i" designator meant a signed native integer (32-bits).
    I don't see why this should fail.

    Also, if I use the "l" designator to indicate a signed long integer (again
    32-bits). Everything works.

    [ 0xFFFFFFFF ].pack("l")

    Shouldn't "i" and "l" act the same if the native size of an integer is
    32-bits?
    Dale Martenson, Feb 18, 2004
    #1
    1. Advertising

  2. Hi,

    In message "Ruby 1.8.1: Unexpected "pack" result"
    on 04/02/19, Dale Martenson <> writes:

    | [ 0xFFFFFFFF ].pack("i")
    |
    |In 1.8.1, this raises a "Range Error: bignum to big to convert into 'int'".

    Fixed in the CVS HEAD.

    matz.
    Yukihiro Matsumoto, Feb 18, 2004
    #2
    1. Advertising

  3. Dale Martenson

    Ara.T.Howard Guest

    On Thu, 19 Feb 2004, Dale Martenson wrote:

    > Date: Thu, 19 Feb 2004 00:05:53 +0900
    > From: Dale Martenson <>
    > Newsgroups: comp.lang.ruby
    > Subject: Ruby 1.8.1: Unexpected "pack" result
    >
    > I noticed something that does seem correct. In an application I have been
    > shipping, I pack value in order to perform IOCTL calls against a device. I
    > hit a problem when using Ruby 1.8.1. The following worked in 1.6.7 and
    > 1.8.0:
    >
    > [ 0xFFFFFFFF ].pack("i")
    >
    > In 1.8.1, this raises a "Range Error: bignum to big to convert into 'int'".
    >
    > I understood that an "i" designator meant a signed native integer (32-bits).
    > I don't see why this should fail.
    >
    > Also, if I use the "l" designator to indicate a signed long integer (again
    > 32-bits). Everything works.
    >
    > [ 0xFFFFFFFF ].pack("l")
    >
    > Shouldn't "i" and "l" act the same if the native size of an integer is
    > 32-bits?


    well, it looks like the orginal code was the problem:


    ~ > cat int_max.rb

    require 'tmpdir'
    require 'ftools'

    c_prog = <<-code
    #include <limits.h>
    main(){int i = INT_MAX; write(1,&i,sizeof(int));}
    code

    tmp = Dir::tmpdir

    a_c = File.join tmp, 'a.c'
    a_out = File.join tmp, 'a.out'
    out = File.join tmp, 'out'

    open(a_c, 'w'){|f| f.write c_prog}
    system "cc #{ a_c } -o #{ a_out }"
    system "#{ a_out } > #{ out }"

    int_max = IO.read(out).unpack('i').first

    p(int_max)
    p((2 ** 31) - 1)
    p(0xFFFFFFFF)
    p((2 ** 32) - 1)


    File.rm_f a_c
    File.rm_f a_out
    File.rm_f out

    ~ > ruby int_max.rb

    2147483647
    2147483647
    4294967295
    4294967295



    you only get 31 bits for signed ints.

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ===============================================================================
    Ara.T.Howard, Feb 18, 2004
    #3
  4. Dale Martenson

    Ara.T.Howard Guest

    On Wed, 18 Feb 2004, Ara.T.Howard wrote:

    <snip>

    this version is even more informative...

    ~ > cat int_max.rb

    require 'tmpdir'
    require 'ftools'

    c_prog = <<-code
    #include <limits.h>
    main(){int i = INT_MAX; write(1,&i,sizeof(int));}
    code

    tmp = Dir::tmpdir

    a_c = File.join tmp, 'a.c'
    a_out = File.join tmp, 'a.out'
    out = File.join tmp, 'out'

    open(a_c, 'w'){|f| f.write c_prog}
    system "cc #{ a_c } -o #{ a_out }"
    system "#{ a_out } > #{ out }"

    int_max = IO.read(out).unpack('i').first

    p(int_max)
    p((2 ** 31) - 1)
    p("%032.32b" % int_max)

    p(0xFFFFFFFF)
    p((2 ** 32) - 1)
    p("%032.32b" % 0xFFFFFFFF)


    File.rm_f a_c
    File.rm_f a_out
    File.rm_f out



    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ===============================================================================
    Ara.T.Howard, Feb 18, 2004
    #4
    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. Mike Pemberton

    unexpected result using std::list

    Mike Pemberton, Oct 14, 2003, in forum: C++
    Replies:
    3
    Views:
    342
    Dan Cernat
    Oct 14, 2003
  2. Tim Jones
    Replies:
    0
    Views:
    378
    Tim Jones
    Jan 31, 2004
  3. Michael Tan
    Replies:
    32
    Views:
    973
    Ara.T.Howard
    Jul 21, 2005
  4. Andy Tolle
    Replies:
    7
    Views:
    229
    Andy Tolle
    Nov 15, 2010
  5. Alexander Farber

    pack 'C3U*' not same as pack 'C3(xC)*'

    Alexander Farber, Jun 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    136
    Ilmari Karonen
    Jun 23, 2005
Loading...

Share This Page