How to convert binary array into integer

(hsun) #1

Hi,

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

for example: 00 00 00 0A => 10

thanks

(J-Van) #2

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

(Ara.T.Howard) #3

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

   harp:~ > ruby a.rb
   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

===============================================================================

(Zach Dennis) #4

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

(Simon Kröger) #5

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: