Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > unpack 4 bytes to a signed integer

Reply
Thread Tools

unpack 4 bytes to a signed integer

 
 
w wg
Guest
Posts: n/a
 
      01-15-2007
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

 
Reply With Quote
 
 
 
 
Mike Stok
Guest
Posts: n/a
 
      01-15-2007

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 <(E-Mail Removed)>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.





 
Reply With Quote
 
 
 
 
w wg
Guest
Posts: n/a
 
      01-15-2007
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 <(E-Mail Removed)>:
>
> 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 <(E-Mail Removed)>
> http://www.stok.ca/~mike/
>
> The "`Stok' disclaimers" apply.
>
>
>
>
>
>



--
--
WenGe Wang

 
Reply With Quote
 
Erik Veenstra
Guest
Posts: n/a
 
      01-15-2007
s = 0xFF.chr * 4
i = s.unpack("L").shift

p i
p 2**32-1


 
Reply With Quote
 
Erik Veenstra
Guest
Posts: n/a
 
      01-15-2007
s = 0xFF.chr * 4
i = s.unpack("L").shift

p i
p 2**32-1


 
Reply With Quote
 
Mike Stok
Guest
Posts: n/a
 
      01-15-2007

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 <(E-Mail Removed)>:
>>
>> 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 <(E-Mail Removed)>
>> http://www.stok.ca/~mike/
>>
>> The "`Stok' disclaimers" apply.
>>
>>
>>
>>
>>
>>

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


--

Mike Stok <(E-Mail Removed)>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.





 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      01-15-2007
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 , 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

 
Reply With Quote
 
w wg
Guest
Posts: n/a
 
      01-16-2007
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


 
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
Re: A portable code to create a 4-bytes Big Endian twos complement signed integer in a misaligned address Tim Rentsch C Programming 0 03-19-2011 04:48 PM
Unpack Expects the Wrong Number of Bytes Timothy Crone Python 2 07-18-2009 02:55 PM
unpack signed short in network (big-endian) byte order baumanj@gmail.com Ruby 11 04-10-2006 03:19 PM
conversion of signed integer to unsigned integer junky_fellow@yahoo.co.in C Programming 14 06-18-2005 02:29 PM
assembling a signed integer from bytes Steve Perl Misc 0 01-08-2005 08:54 PM



Advertisments