[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Converting Bytes to a Negative Integer

Harris Reynolds

2/15/2007 3:32:00 PM

I really like the to_signed proc idea at the bottom of this message... the procedure doesn't work however in all cases... note the Ruby code below:bits = 32max_unsigned = 2 ** bitsmax_signed = 2 ** (bits - 1)to_signed = proc { |n| (n >= max_signed) ? n - max_unsigned : n }x = -123s = [x].pack("N")puts to_signed[s.unpack("N").first]y = 7777777s = [y].pack("N")puts to_signed[s.unpack("N").first]# try converting 7777777 when you already have the bytesz = ""[129, 237, 173, 241].each { |e| z << e }puts to_signed[z.unpack("N").first]# OUTPUT>ruby signed_test.rb-1237777777-2115129871Coverting the bytes of 7777777 yields a pretty nasty negative number in this case.~harrisOn 2/14/07, James Edward Gray II <james / grayproductions.net> wrote: > On Feb 14, 2007, at 2:27 PM, Harris Reynolds wrote: > > Let's say I have 4 bytes: > > > > 255 255 255 236 > > >> bytes = [255, 255, 255, 236].map { |b| b.chr }.join > => "\377\377\377\354" > > > The binary representation of this is: > > > > 11111111 > > 11111111 > > 11111111 > > 11101100 > > >> bytes.unpack("B*").first.scan(/[01]{8}/) > => ["11111111", "11111111", "11111111", "11101100"] > > > The decimal number should be -20. Does anyone know how to convert > > these 4 bytes into an Integer? > > This is where I had trouble. I found the following: > > >> bytes.reverse.unpack("l*").first > => -20 > > This is tied to my processor though. I had to reverse the bytes > because they are not in the order my machine expects. > > unpack() has directives for when the order is known, but all of those > seemed to be unsigned. > > I'm anxious to see the cross-platform solution for this... Joel VanDerWerf had something that I'll be adapting if I need it elsewhere. x = -123 s = [x].pack("N") bits = 32 max_unsigned = 2 ** bits max_signed = 2 ** (bits - 1) to_signed = proc { |n| (n >= max_signed) ? n - max_unsigned : n } puts to_signed[s.unpack("N").first] I need it for unsigned->signed words, so: x = -123 s = [x].pack("n") bits = 16 max_unsigned = 2 ** bits max_signed = 2 ** (bits - 1) to_signed = proc { |n| (n >= max_signed) ? n - max_unsigned : n } puts to_signed[s.unpack("n").first] -austin -- Austin Ziegler * halostatue@gmail.com * http://www.halo... * austin@halostatue.ca * http://www.halo...feed/ * austin@zieglers.ca____________________________________________________________________________________Cheap talk?Check out Yahoo! Messenger's low PC-to-Phone call rates.http://voice... ____________________________________________________________________________________Food fight? Enjoy some healthy debate in the Yahoo! Answers Food & Drink Q&A.http://answers.yahoo.com/dir/?link=list&sid...

1 Answer

Austin Ziegler

2/15/2007 6:34:00 PM

0

On 2/15/07, Harris Reynolds <hreynolds2@yahoo.com> wrote:
> # try converting 7777777 when you already have the bytes
> z = ""
> [129, 237, 173, 241].each { |e| z << e }
> puts to_signed[z.unpack("N").first]

The bytes are wrong. The bytes for 7,777,777 in "N" format are [0,
118, 173, 241].

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca