Home > Archive > Ruby > August 2005 > how to convert binary array into integer
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to convert binary array into integer
|
|
|
| Hi,
Does anyone know the api to convert a binary array into integer:
for example: 00 00 00 0A =3D> 10
thanks=20
| |
| Joe Van Dyk 2005-08-31, 7:00 pm |
| On 8/31/05, hsun <sunh11373@hotmail.com> wrote:
> Hi,
>=20
> Does anyone know the api to convert a binary array into integer:
>=20
> for example: 00 00 00 0A =3D> 10
Have you looked at String#unpack?
| |
| Ara.T.Howard 2005-08-31, 7:00 pm |
| 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
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
========================================
=======================================
| |
| Zach Dennis 2005-08-31, 7:00 pm |
| Joe Van Dyk wrote:
> On 8/31/05, hsun <sunh11373@hotmail.com> wrote:
>
>
>
> Have you looked at String#unpack?
"\000\000 A".unpack( "f" ) => 10.0
Zach
| |
| Simon Kröger 2005-08-31, 7:00 pm |
| Zach Dennis wrote:
> Joe Van Dyk wrote:
>
>
>
>
> "\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
|
|
|
|
|