Re: Converting table to a number?
- To: mathgroup at smc.vnet.net
- Subject: [mg2112] Re: Converting table to a number?
- From: villegas (Robert Villegas)
- Date: Wed, 4 Oct 1995 01:52:46 -0400
- Organization: Wolfram Research, Inc.
> 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)
If you just want something to display as the binary sequence, you can
join the digits into a string:
In[4]:= StringJoin[ToString /@ { 1, 0, 1, 1, 0, 1, 0, 0 }]
Out[4]= 10110100
or use a formatting function that displays its elements in a horizontal
sequence:
In[5]:= SequenceForm @@ { 1, 0, 1, 1, 0, 1, 0, 0 }
Out[5]= 10110100
If you want to form the actual integer, you can use the ^^ operator:
In[6]:= 2^^10110100
Out[6]= 180
Here's a function that makes it easy to convert a base and a string or list
of digits to the represented integer:
-----------------------------------------------------------------------------
BasePower[base_, digits_String] := ToExpression[ToString[base] <> "^^" <>
digits]
BasePower[base_, digits:{__String}] := BasePower[base, StringJoin[digits]]
$letters = FromCharacterCode /@ Range[64 + 1, 64 + 26]
$todigits = Thread[Range[10, 35] -> $letters]
BasePower[base_, digits:{__Integer}] :=
BasePower[base, ToString /@ (digits /. Thread[Range[10, 35] -> $letters]) ]
-----------------------------------------------------------------------------
It works up to base 36. You can give digits as actual integers, or as
letters:
In[6]:= BasePower[16, {13, 12, 11}]
Out[6]= 3531
In[7]:= BasePower[16, "DCB"]
Out[7]= 3531
In[8]:= BasePower[16, {"D", "C", "B"}]
Out[8]= 3531
Robby Villegas