MathGroup Archive 1992

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: converting a number in base b to a number in base 10 using ^^ in Mathematica

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: Re: converting a number in base b to a number in base 10 using ^^ in Mathematica
  • From: David Withoff <withoff>
  • Date: Thu, 26 Mar 1992 16:37:12 -0600

In response to Prof. Gaylord's note:

> base2tobase10conversion[n_Integer] := 2^^n
> General::digit:
>    Digit at position 1 in n
>      is too large to be used in
>     base 2.
>
> it seems that the ^^ operator absolutely insists on being immediately
> followed by an integer.
>  
> can someone suggest another way (there must be; there's always another way
> to do almost everything in M) to convert a number in base b to base 10
> other than using ^^.
>  
> i find it hard to believe that M does not have a built-in function for
> doing this but i can't find it in the book.

You could do something like this:

In[4]:= c[n_] := ToExpression[StringJoin["2^^", ToString[n]]]

In[5]:= c[101]

Out[5]= 5

The ^^ operation is one of a very small number of operations (the other
ones being things like ??) that are handled by the parser rather than
the evaluator.

One difficulty with implementing ^^ as a separate function is handling
of bases greater than 10:

In[6]:= 16^^3fa7

Out[6]= 16295

If 3fa7 appeared as a function argument it would be parsed as
Times[3, fa7] rather than as a string of characters, and it seems
a bit awkward to have to enter a number as a string in the first place.

I was a bit surprised myself when I first realized that there wasn't
a built-in function to do this, and if not for pesky design issues
(like how to handle bases bigger than 10) there probably would be.

Dave Withoff
withoff at wri.com





  • Prev by Date: Re: Ceiling & Floor
  • Next by Date: converting from base to base...
  • Previous by thread: Re: converting a number in base b to a number in base 10 using ^^ in Mathematica
  • Next by thread: Why won't MMA simplify parts of expressions?