Hot answers tagged number-bases
18
To understand what's happening, the difference between evaluation and parsing needs to be made clear:
parsing means taking the string (the text) input to Mathematica and converting it to some internal representation of a Mathematica expression
evaluation means taking a Mathematica expression and transforming it according to some rules the evaluator knows ...
14
You can see that the base never survives to the evaluation stage by trying for example
16^^98 // Unevaluated // AtomQ
True
16^^98 // Unevaluated // Head
Integer
Trace[16^^98, TraceInternal -> True]
{}
It's more or less like a box formatting rule. The Front End sends the literal structure to the kernel, it first builds up the ...
14
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.
12
The reason is that the notation base^^digits is interpreted at parsing time, not evaluation time. I explained the difference in this answer.
You can use FromDigits instead:
fromBaseTwo = FromDigits[#, 2]&
fromBaseTwo["10011"]
Note that I used a string as input. FromDigits works both with strings and lists of digits.
6
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], ...
6
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
...
3
What is important here is to distinguish between data and representation. When you input an integer, you actually input a representation of the integer. That is, even without specifying the base, you don't enter the integer 42 (you would be hard-pressed to do that), but the decimal representation of the integer, consisting of the two digits 4 and 2, in that ...
1
You could do something like this:
toBaseString[n_?NumericQ, b_Integer?Positive] :=
First @ StringSplit @ ToString @ BaseForm[n, b]
toBaseString[365.7, 5]
"2430.32222"
I missed that you didn't want strings. Perhaps you want this?:
toBasePlain[n_?NumericQ, b_Integer] /; 11 > b > 0 :=
N @ FromDigits @ RealDigits[n, b]
toBasePlain[365.7, ...
Only top voted, non community-wiki answers of a minimum length are eligible
