# Binary numbers

**URL:** <https://www.ruby-forum.com/t/binary-numbers/100912>\
**Category:** Ruby\
**Created:** [August 1, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/binary-numbers/100912 "2006-08-01T22:49:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimpastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kimpastro](https://www.ruby-forum.com/u/kimpastro)\
**Post date:** [August 1, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/binary-numbers/100912/1 "2006-08-01T22:49:23Z")

</div>

Hi,

I have some data with binary numbers.  
e.g. a 3 byte string could be 0x01216f which is 74095 decimal.  
To do this conversion in ruby I’ve come up with:

def binum(bs)  
n = e = 0  
bs.reverse.each\_byte do |c|  
n += c \* 256 \*\* e  
e += 1  
end  
n  
end

bs = ‘’  
bs \<\< 0x01 \<\< 0x21 \<\< 0x6f

puts binum(bs) =\> 74095

Now some data are signed binary numbers stored as two’s complement.  
e.g. 0xfede91 which is -74095 decimal.

How can I do this conversion in ruby?

Best regards,  
Kim

---

<div class="post-metadata">

**Author:** ![kimpastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kimpastro](https://www.ruby-forum.com/u/kimpastro)\
**Post date:** [August 1, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/binary-numbers/100912/2 "2006-08-01T23:35:29Z")

</div>

On Wednesday, August 02, 2006, at 5:46 AM, Kim P. wrote:

> ```
> e += 1
> 
> ```
> 
> e.g. 0xfede91 which is -74095 decimal.
> 
> How can I do this conversion in ruby?

def binum(bs)  
result = 0  
bs.unpack(“cCC”).each {|b| result = (result \<\< 8) | b }  
result  
end

bs = ‘’  
bs \<\< 0x01 \<\< 0x21 \<\< 0x6f

puts binum(bs)

bs = ‘’  
bs \<\< 0xfe \<\< 0xde \<\< 0x91

puts binum(bs)

The above code extracts the first byte as a signed 8-bit integer and the  
rest of the bytes as unsigned 8-bit integers. Each iteration through the  
loop shift the result to construct the proper value.

–Dale M.

---

<div class="post-metadata">

**Author:** ![kimpastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kimpastro](https://www.ruby-forum.com/u/kimpastro)\
**Post date:** [August 1, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/binary-numbers/100912/3 "2006-08-01T23:41:52Z")

</div>

On Aug 1, 2006, at 4:33 PM, Dale M. wrote:

> def binum(bs)  
> result = 0  
> bs.unpack(“cCC”).each {|b| result = (result \<\< 8) | b }  
> result  
> end

That’s just crying out for inject():

bs.unpack(“cCC”).inject(0) { |res, b| (res \<\< 8) | b }

😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![kimpastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kimpastro](https://www.ruby-forum.com/u/kimpastro)\
**Post date:** [August 2, 2006, 12:06am UTC](https://www.ruby-forum.com/t/binary-numbers/100912/4 "2006-08-02T00:06:21Z")

</div>

James Edward G. II wrote:

> bs.unpack(“cCC”).inject(0) { |res, b| (res \<\< 8) | b }
> 
> 😉
> 
> James Edward G. II  
> Thanks,  
> ps. just bought your ruby quiz book 🙂

---

<div class="post-metadata">

**Author:** ![kimpastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kimpastro](https://www.ruby-forum.com/u/kimpastro)\
**Post date:** [August 1, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/binary-numbers/100912/5 "2006-08-01T23:49:25Z")

</div>

Dale M. wrote:

> > bs.reverse.each\_byte do |c|
> 
> end
> 
> The above code extracts the first byte as a signed 8-bit integer and the  
> rest of the bytes as unsigned 8-bit integers. Each iteration through the  
> loop shift the result to construct the proper value.
> 
> –Dale M.

Great!  
Thanks  
Kim

BTW, the length of the string may vary from 1 to 8. I’ll have to adjust  
for that.

---

<div class="post-metadata">

**Author:** ![kimpastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kimpastro](https://www.ruby-forum.com/u/kimpastro)\
**Post date:** [August 2, 2006, 3:04am UTC](https://www.ruby-forum.com/t/binary-numbers/100912/6 "2006-08-02T03:04:56Z")

</div>

On 8/2/06, Kim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > n = e = 0  
> > > puts binum(bs) =\> 74095  
> > > result  
> > > puts binum(bs)
> 
> BTW, the length of the string may vary from 1 to 8. I’ll have to adjust  
> for that.

For variable length;

c.unpack(“cC\*”).inject(0) { |res, b| (res \<\< 8) | b }
