RE: Binary conversion

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

One way might be to do this:

atom binary, sum
binary = {0,0,0,2}
sum = 0

for x = 1 to length(binary) do
   sum += (power(10, length(binary)-x))*binary[x]
end for

this would convert any langth of binary into a decimal number (replace ten 
within the loop to convert to a different base)

so 0,0,0,2 = 0*10^3 + 0*10^2 + 0*10^1 + 2*10^0 = 2

additionally..
1,2,3,4,5,6 = 1*10^5 + 2*10^4 + 3*10^3 + 4*10^2 + 5*10^1 + 6*10^0 = 12345

Note: This will only change a sequence of "digits" into one atom (or 
number), not convert from one base into another.


>From: rudy toews <rltoews at ilos.net>
>Reply-To: EUforum at topica.com
>To: EUforum <EUforum at topica.com>
>Subject: RE: Binary conversion
>Date: Thu, 15 Aug 2002 22:41:17 +0000
>
>
>Jonas  Temple wrote:
> > Okay, I asked this a while back and now I've got a slightly different
> > problem.
> >
> > If I have a sequence of {0,0,0,2} that is supposed to represent a binary
> >
> > number, how do I convert that to an atom?
> >
> > Jonas
> >
> >
>hi Jonas
>i did not see the orginal posting.
>but would a table be helpful?
>since the smallest element in Euphoria is an atom then a table of 32
>entrys of the 32 different bit positions would be needed.
>
>using 'or', 'and' or 'xor' should work
>
>receiver = receiver or'd with table entry(input position)
>
>this is my first thought on it. working nights makes one groggy.
>later
>rudy
>
>
>lotterywars
>
>
>
>

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

Search



Quick Links

User menu

Not signed in.

Misc Menu