Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to convert binary array into integer

Reply
Thread Tools

how to convert binary array into integer

 
 
hsun
Guest
Posts: n/a
 
      08-31-2005
Hi,

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

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

thanks=20





 
Reply With Quote
 
 
 
 
Joe Van Dyk
Guest
Posts: n/a
 
      08-31-2005
On 8/31/05, hsun <(E-Mail Removed)> 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?


 
Reply With Quote
 
 
 
 
Ara.T.Howard
Guest
Posts: n/a
 
      08-31-2005
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
================================================== =============================



 
Reply With Quote
 
Zach Dennis
Guest
Posts: n/a
 
      08-31-2005
Joe Van Dyk wrote:
> On 8/31/05, hsun <(E-Mail Removed)> 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


 
Reply With Quote
 
Simon Kröger
Guest
Posts: n/a
 
      08-31-2005
Zach Dennis wrote:

> Joe Van Dyk wrote:
>
>> On 8/31/05, hsun <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to convert byte array into integer msosno01@gmail.com C++ 1 07-20-2006 10:53 PM
Convert binary char array to integer with reordering Dave C Programming 16 10-26-2005 06:39 AM
how to convert "BigInteger" into "Integer"? how to print out a BigInteger binary value? nick Java 1 10-26-2004 02:45 PM
how to convert "BigInteger" into "Integer"? how to print out a BigInteger binary value? nick Java 0 10-26-2004 08:18 AM



Advertisments