Re: Converting table to a number?
- To: mathgroup at smc.vnet.net
- Subject: [mg2113] Re: Converting table to a number?
- From: Jorma Virtamo <jorma.virtamo at vtt.fi>
- Date: Wed, 4 Oct 1995 01:52:57 -0400
olee at ripco.com (O. Lee) wrote:
>
>
> Can anyone help me solve this problem?
> I have a list of numbers, such as this:
>
> { 1, 0, 1, 1, 0, 1, 0, 0 }
>
> and I want to convert it into this:
>
> 10110100 (binary)
>
> Is there a command in Mathematica that would do this?
>
>
To my knowledge there is no single command to do that, but
of course it's easy to construct one's own function
In[1]:= tobin[lst_] := Module[{e=1,s=0},
(s+=#e;e*=2)& /@ Reverse[lst];
Return[s]]
Let's test it
In[2]:= lst = { 1, 0, 1, 1, 0, 1, 0, 0 };
In[3]:= tobin[lst]
Out[3]= 180
In[4]:= IntegerDigits[%,2]
Out[4]= {1, 0, 1, 1, 0, 1, 0, 0}
Another possibility is to play with the strings:
In[5]:= ToExpression["2^^"~StringJoin~FromCharacterCode[lst+48]]
Out[5]= 180
-- Jorma Virtamo
============================================================
Jorma Virtamo
VTT Information Technology / Telecommunications
P.O. Box 1202, FIN-02044 VTT, Finland
phone: +358 0 456 5612 fax: +358 0 455 0115
email: jorma.virtamo at vtt.fi web: http://www.vtt.fi/tte/
============================================================