# Re: Converting Bytes to a Negative Integer

**URL:** <https://www.ruby-forum.com/t/re-converting-bytes-to-a-negative-integer/85695>\
**Category:** Ruby\
**Created:** [February 15, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/re-converting-bytes-to-a-negative-integer/85695 "2007-02-15T16:33:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harris\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@Harris\_R](https://www.ruby-forum.com/u/Harris_R)\
**Post date:** [February 15, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/re-converting-bytes-to-a-negative-integer/85695/1 "2007-02-15T16:33:25Z")

</div>

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 = 32  
max\_unsigned = 2 \*\* bits  
max\_signed = 2 \*\* (bits - 1)  
to\_signed = proc { |n| (n \>= max\_signed) ? n - max\_unsigned : n }

x = -123  
s = [x].pack(“N”)  
puts to\_signed[s.unpack(“N”).first]

y = 7777777  
s = [y].pack(“N”)  
puts to\_signed[s.unpack(“N”).first]

# 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]

# OUTPUT

> ruby signed\_test.rb  
> -123  
> 7777777  
> -2115129871

Coverting the bytes of 7777777 yields a pretty nasty negative number in  
this case.

~harris

On 2/14/07, James Edward G. II \<james / [grayproductions.net](http://grayproductions.net)\> wrote:

> > 11111111  
> > This is where I had trouble. I found the following:  
> > 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 Z. \* removed\_email\_address@domain.invalid \* [http://www.halostatue.ca/](http://www.halostatue.ca/)

- removed\_email\_address@domain.invalid \* [You are in a maze of twisty little passages, all alike. // halo • statue](http://www.halostatue.ca/feed/)
- removed\_email\_address@domain.invalid

* * *

Cheap talk?  
Check out Yahoo! Messenger’s low PC-to-Phone call rates.  
[http://voice.yahoo.com](http://voice.yahoo.com)

---

<div class="post-metadata">

**Author:** ![Harris\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@Harris\_R](https://www.ruby-forum.com/u/Harris_R)\
**Post date:** [February 15, 2007, 7:34pm UTC](https://www.ruby-forum.com/t/re-converting-bytes-to-a-negative-integer/85695/2 "2007-02-15T19:34:51Z")

</div>

On 2/15/07, Harris R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
