unpack 4 bytes to a signed integer

Discussion in 'Ruby' started by w wg, Jan 15, 2007.

  1. w wg

    w wg Guest

    Hi
    I' m using unpack to convert 4 bytes to local integer, but ruby just
    supply the "N" modifer which means unsigned long integer.

    My questions is :
    How to unpack 4 bytes to a signed integer ?

    Thank you.

    --
    wwg
     
    w wg, Jan 15, 2007
    #1
    1. Advertising

  2. w wg

    Mike Stok Guest

    On 15-Jan-07, at 8:29 AM, w wg wrote:

    > Hi
    > I' m using unpack to convert 4 bytes to local integer, but ruby just
    > supply the "N" modifer which means unsigned long integer.
    >
    > My questions is :
    > How to unpack 4 bytes to a signed integer ?



    There are "i" and "I" for signed and unsigned integer respectively,
    which deals in the local size of integer.

    Hope this helps,

    Mike

    --

    Mike Stok <>
    http://www.stok.ca/~mike/

    The "`Stok' disclaimers" apply.
     
    Mike Stok, Jan 15, 2007
    #2
    1. Advertising

  3. w wg

    w wg Guest

    But the "i" and "I" modifer don't care bytes order.
    I'm reading data from network using IO#sysread, I need receive integer
    , not unsigned integer.

    Thank you for your reply.

    2007/1/15, Mike Stok <>:
    >
    > On 15-Jan-07, at 8:29 AM, w wg wrote:
    >
    > > Hi
    > > I' m using unpack to convert 4 bytes to local integer, but ruby just
    > > supply the "N" modifer which means unsigned long integer.
    > >
    > > My questions is :
    > > How to unpack 4 bytes to a signed integer ?

    >
    >
    > There are "i" and "I" for signed and unsigned integer respectively,
    > which deals in the local size of integer.
    >
    > Hope this helps,
    >
    > Mike
    >
    > --
    >
    > Mike Stok <>
    > http://www.stok.ca/~mike/
    >
    > The "`Stok' disclaimers" apply.
    >
    >
    >
    >
    >
    >



    --
    --
    WenGe Wang
     
    w wg, Jan 15, 2007
    #3
  4. s = 0xFF.chr * 4
    i = s.unpack("L").shift

    p i
    p 2**32-1
     
    Erik Veenstra, Jan 15, 2007
    #4
  5. s = 0xFF.chr * 4
    i = s.unpack("L").shift

    p i
    p 2**32-1
     
    Erik Veenstra, Jan 15, 2007
    #5
  6. w wg

    Mike Stok Guest

    On 15-Jan-07, at 10:10 AM, w wg wrote:

    > But the "i" and "I" modifer don't care bytes order.
    > I'm reading data from network using IO#sysread, I need receive integer
    > , not unsigned integer.
    >
    > Thank you for your reply.


    OK then, what about "V"? "treat 4 characters as an unsigned long in
    little-endian byte order"

    The N uses network byte order "big-endian"

    Mike


    >
    > 2007/1/15, Mike Stok <>:
    >>
    >> On 15-Jan-07, at 8:29 AM, w wg wrote:
    >>
    >> > Hi
    >> > I' m using unpack to convert 4 bytes to local integer, but ruby

    >> just
    >> > supply the "N" modifer which means unsigned long integer.
    >> >
    >> > My questions is :
    >> > How to unpack 4 bytes to a signed integer ?

    >>
    >>
    >> There are "i" and "I" for signed and unsigned integer respectively,
    >> which deals in the local size of integer.
    >>
    >> Hope this helps,
    >>
    >> Mike
    >>
    >> --
    >>
    >> Mike Stok <>
    >> http://www.stok.ca/~mike/
    >>
    >> The "`Stok' disclaimers" apply.
    >>
    >>
    >>
    >>
    >>
    >>

    >
    >
    > --
    > --
    > WenGe Wang
    >
    >


    --

    Mike Stok <>
    http://www.stok.ca/~mike/

    The "`Stok' disclaimers" apply.
     
    Mike Stok, Jan 15, 2007
    #6
  7. w wg wrote:
    > Hi
    > I' m using unpack to convert 4 bytes to local integer, but ruby just
    > supply the "N" modifer which means unsigned long integer.
    >
    > My questions is :
    > How to unpack 4 bytes to a signed integer ?


    The best way I've found is to unpack with N (to get the swapping right)
    and then do some arithmetic to interpret the unsigned value as signed:

    x = -123
    s = [x].pack("N")
    # Note that for _pack_ there is no need for a
    # special signed version of N

    p s # ==> "\377\377\377\205"

    length = 32
    mid = 2**(length-1)
    max_unsigned = 2**length
    to_signed = proc {|n| (n>=mid) ? n - max_unsigned : n}

    p to_signed[s.unpack("N").first] # ==> -123



    This is all very hard for me to remember, so I've written a library to
    do it, bit-struct (http://redshift.sourceforge.net/bit-struct). This
    makes life easier:

    require 'bit-struct'

    class Packet < BitStruct
    signed :x, 32
    end

    pkt = Packet.new
    pkt.x = -123

    p pkt.to_s # ==> "\377\377\377\205"
    p pkt.x # ==> -123

    # given string data from a network:
    pkt2 = Packet.new("\377\377\377\205")
    p pkt2.x # ==> -123

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jan 15, 2007
    #7
  8. w wg

    w wg Guest

    I tried your codes, it works exactly as what I expected. I havn't
    tried your library, but I think it should get the correct result too.

    Thank you.

    > The best way I've found is to unpack with N (to get the swapping right)
    > and then do some arithmetic to interpret the unsigned value as signed:
    >
    > x = -123
    > s = [x].pack("N")
    > # Note that for _pack_ there is no need for a
    > # special signed version of N
    >
    > p s # ==> "\377\377\377\205"
    >
    > length = 32
    > mid = 2**(length-1)
    > max_unsigned = 2**length
    > to_signed = proc {|n| (n>=mid) ? n - max_unsigned : n}
    >
    > p to_signed[s.unpack("N").first] # ==> -123
     
    w wg, Jan 16, 2007
    #8
    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. Replies:
    14
    Views:
    2,174
    CBFalconer
    Jun 18, 2005
  2. Timothy Crone

    Unpack Expects the Wrong Number of Bytes

    Timothy Crone, Jul 18, 2009, in forum: Python
    Replies:
    2
    Views:
    400
    Ross Ridge
    Jul 18, 2009
  3. Tim Rentsch
    Replies:
    0
    Views:
    479
    Tim Rentsch
    Mar 19, 2011
  4. Replies:
    11
    Views:
    405
  5. Steve
    Replies:
    0
    Views:
    115
    Steve
    Jan 8, 2005
Loading...

Share This Page