Professional Web Applications Themes

Ruby 1.8.1: Unexpected "pack" result - Ruby

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 ...

  1. #1

    Default 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?



    Dale Guest

  2. #2

    Default Re: Ruby 1.8.1: Unexpected "pack" result

    Hi,

    In message "Ruby 1.8.1: Unexpected "pack" result"
    on 04/02/19, Dale Martenson <com> 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 Guest

  3. #3

    Default Re: Ruby 1.8.1: Unexpected "pack" result

    On Thu, 19 Feb 2004, Dale Martenson wrote:
     

    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 Guest

  4. #4

    Default Re: Ruby 1.8.1: Unexpected "pack" result

    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 Guest

Similar Threads

  1. #26162 [NEW]: $a="0abcdefg";if ($a==0) echo "OK"; result is "OK" ?!
    By zhuminglun at yahoo dot com dot cn in forum PHP Development
    Replies: 0
    Last Post: November 7th, 12:04 PM
  2. Replies: 0
    Last Post: November 7th, 11:45 AM
  3. Replies: 0
    Last Post: October 13th, 11:09 PM
  4. #12029 [Com]: "--with-apxs" can cause the error "MySQL: Unable to save result set in"
    By sietzepost at streetwisedd dot nl in forum PHP Development
    Replies: 1
    Last Post: October 3rd, 09:21 AM
  5. Replies: 2
    Last Post: July 2nd, 12:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139