15
$\begingroup$

I'm trying to use baseform to convert numbers in base 10 to base n, how can I make the convertion between, say base 2 to base n?

Baseform seems to always think that the base in expr BaseForm[Expr, n] is always 10.

$\endgroup$
2
  • 1
    $\begingroup$ Maybe using FromDigits ? $\endgroup$ Commented Jun 10, 2012 at 12:09
  • $\begingroup$ Thanks, it's also a viable alternative. $\endgroup$
    – Red Banana
    Commented Jun 10, 2012 at 12:14

4 Answers 4

22
$\begingroup$

It should be possible to use notation of the form base^^number inside the BaseForm expression like this:

BaseForm[2^^10101,14]

There are some similar examples under Properties and Relations in the documentation for BaseForm.

$\endgroup$
5
  • $\begingroup$ Thanks buddy. I've read about it yesterday but it was kinda unclear to me. $\endgroup$
    – Red Banana
    Commented Jun 10, 2012 at 12:13
  • 4
    $\begingroup$ Whilst looking inside a discarded Stanford Bunny, I discovered a note saying that BaseForm only works if n is less than 37. $\endgroup$ Commented Jun 11, 2012 at 10:57
  • $\begingroup$ @image_doctor I_swear_ I once was calling Stephen W. names b/c of that $\endgroup$ Commented Jun 12, 2012 at 3:42
  • 2
    $\begingroup$ @image_doctor of course as that takes you to the limit of English language keyboards. Besides, base 36 allows you to write all the rude messages you wish, unlike base 16. What is DEADBEEF, anyway? $\endgroup$
    – rcollyer
    Commented Jun 13, 2012 at 3:41
  • $\begingroup$ @rcollyer :) and I thought the answer was FORTYTWO. $\endgroup$ Commented Jun 13, 2012 at 8:25
16
$\begingroup$

Here are the above elements wrapped up in function which pulls together the various, or user defined, output forms and lets you switch from any base to any base:

Clear[BaseTranslator];
Options[BaseTranslator] = {BTForm -> BaseForm};
BaseTranslator[number_, base1_, base2_, 
  OptionsPattern[]] := (OptionValue@BTForm)[
  FromDigits[ToString[number], base1], base2]

And some sample usage:

BaseTranslator[100,10,4]

12104

BaseTranslator[100, 10, 4 ,BTForm -> IntegerDigits]

{1, 2, 1, 0}

BaseTranslator[100, 2, 4, BTForm -> IntegerDigits]

{ 1, 0}

BaseTranslator[102, 10, 101, BTForm -> IntegerDigits]

{1, 1}

$\endgroup$
3
  • $\begingroup$ Not much love for this answer .. where does it go wrong ? :) $\endgroup$ Commented Jun 10, 2012 at 20:43
  • 1
    $\begingroup$ Votes are elusive phantoms $\endgroup$ Commented Jun 10, 2012 at 22:37
  • 4
    $\begingroup$ @belisarius :), I've had some success using a Laplacian of Gaussian to improve the definition of the phantom phantom and it is becoming increasingly obvious that the element that was missing from this answer is resolving towards the Stanford Bunny. $\endgroup$ Commented Jun 11, 2012 at 9:49
10
$\begingroup$

Since numbers given in base^^ form automatically parse as regular number, it can be at times useful to pass numbers around as strings. For example:

FromDigits["100010011110011", 2]
17651

Different ways to represent that number:

IntegerDigits[17651, 16]

BaseForm[17651, 2]

IntegerString[17651, 2]

{4, 4, 15, 3}

1000100111100112

"100010011110011"

$\endgroup$
0
2
$\begingroup$

A simplest form for BaseTranslator (https://mathematica.stackexchange.com/users/776/image-doctor):

(*convert stringnumber from base1 to base2*)
convert[stringnumber_, base1_, base2_] :=    
  BaseForm[FromDigits[stringnumber, base1], base2]

convert["19a", 16, 10](*example 1*)

convert["A", 16, 2] (*example *)
 410

 Subscript[1010, 2]
$\endgroup$
1
  • $\begingroup$ ROSU, I formatted your code for you. Please see editing help to learn how to do this yourself. Also, I note that this answer has been down-voted; that is likely because it removes rather than adds functionality to image_doctor's method. $\endgroup$
    – Mr.Wizard
    Commented Jan 17, 2014 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.