Re: binary to decimal conversion

new topic     » goto parent     » topic index » view thread      » older message » newer message

Hello J Reeves,

>Does anyone have a lib that takes 2 bit
>binary numbers and converts to decimal?
>I tried it but its ugly and doesn't work.


I found the following in:
http://rapideuphoria.com/lib_a_b.htm#bits_to_int
Is this what you need


bits_to_int
Syntax: include machine.e
a = bits_to_int(s)
Description: Convert a sequence of binary 1's and 0's into a positive
number. The least-significant bit is s[1].
Comments: If you print s the bits will appear in "reverse" order, but it is
convenient to have increasing subscripts access bits of increasing
significance.
Example:
a = bits_to_int({1,1,1,0,1})
-- a is 23 (binary 10111)

See Also: int_to_bits, operations on sequences

According the above description, it looks like you will need
to reverse the sequence of bits before sending it to
bits_to_int(). Here is what I think would work:

<Untested code>
include machine.e -- for bits_to_int()
include misc.e -- for reverse()
sequence binary
integer int
binary = {0,1} -- 1
int = bits_to_int( reverse( binary ) )
? int
<end untested code>


later,
Lewis Townsend
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

Share information about yourself, create your own public profile at
http://profiles.msn.com.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu