# How to convert binary array into integer

**URL:** <https://rubytalk.org/t/how-to-convert-binary-array-into-integer/20529>\
**Category:** ruby-talk\
**Created:** [31 August 2005 18:54 UTC](https://rubytalk.org/t/how-to-convert-binary-array-into-integer/20529 "2005-08-31T18:54:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsun](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@hsun](https://rubytalk.org/u/hsun)\
**Post date:** [31 August 2005 18:54 UTC](https://rubytalk.org/t/how-to-convert-binary-array-into-integer/20529/1 "2005-08-31T18:54:48Z")

</div>

Hi,

Does anyone know the api to convert a binary array into integer:

for example: 00 00 00 0A =\> 10

thanks

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [31 August 2005 19:12 UTC](https://rubytalk.org/t/how-to-convert-binary-array-into-integer/20529/2 "2005-08-31T19:12:41Z")

</div>

Have you looked at String#unpack?

> **···**
>
> On 8/31/05, hsun \<sunh11373@hotmail.com\> wrote:
> 
> > Hi,
> > 
> > Does anyone know the api to convert a binary array into integer:
> > 
> > for example: 00 00 00 0A =\> 10

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [31 August 2005 19:28 UTC](https://rubytalk.org/t/how-to-convert-binary-array-into-integer/20529/3 "2005-08-31T19:28:53Z")

</div>

harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;p( [0x0, 0x0, 0x0, 0xA].pack('c\*').unpack('N').first )

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;10

hth.

-a

> **···**
>
> On Thu, 1 Sep 2005, hsun wrote:
> 
> > Hi,
> > 
> > Does anyone know the api to convert a binary array into integer:
> > 
> > for example: 00 00 00 0A =\> 10
> > 
> > thanks
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death  
> > Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [31 August 2005 19:32 UTC](https://rubytalk.org/t/how-to-convert-binary-array-into-integer/20529/4 "2005-08-31T19:32:54Z")

</div>

Joe Van Dyk wrote:

> **···**
>
> > On 8/31/05, hsun \<sunh11373@hotmail.com\> wrote:
> > 
> > > Hi,
> > > 
> > > Does anyone know the api to convert a binary array into integer:
> > > 
> > > for example: 00 00 00 0A =\> 10
> > 
> > Have you looked at String#unpack?
> 
> "\000\000 A".unpack( "f" ) =\> 10.0
> 
> Zach

---

<div class="post-metadata">

**Author:** ![Simon\_Kroger](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Simon\_Kroger](https://rubytalk.org/u/Simon_Kroger)\
**Post date:** [31 August 2005 20:10 UTC](https://rubytalk.org/t/how-to-convert-binary-array-into-integer/20529/5 "2005-08-31T20:10:03Z")

</div>

Zach Dennis wrote:

> Joe Van Dyk wrote:
> 
> > > Hi,
> > > 
> > > Does anyone know the api to convert a binary array into integer:
> > > 
> > > for example: 00 00 00 0A =\> 10
> > 
> > Have you looked at String#unpack?
> 
> "\000\000 A".unpack( "f" ) =\> 10.0
> 
> Zach

This was a joke, right?

"\000\000 B".unpack( "f" ) =\> 40.0

if not, i didn't want to be cynical.

you can try this:

a = [0x00,0x00,0x00,0x0A]  
puts a.inject(0){|r, i| r \<\< 8 | i}

cheers

Simon

> **···**
>
> > > On 8/31/05, hsun \<sunh11373@hotmail.com\> wrote:
