how to convert binary array into integer

H

hsun

Hi,

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

for example: 00 00 00 0A =3D> 10

thanks=20
 
A

Ara.T.Howard

Hi,

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

for example: 00 00 00 0A => 10

thanks

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

harp:~ > ruby a.rb
10

hth.

-a
--
===============================================================================
| 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
===============================================================================
 
S

Simon Kröger

Zach said:
"\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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top