Mathematica won't change a number's BaseForm recursively:

63969 // BaseForm[#, 16] & // BaseForm[#, 8] & // BaseForm[#, 2] &

Well, I inspected former answers, most of them treated formatting problems, but none of the anwers given treated the nestable aspect. Maybe I'm wrong, and I risk a duplicate.

(1) I would like to have a nestable function which allows constructs of the form

63969 // base@16 // base@8 // base@2

This feature might be adopted advantageously within iterative functions : NestList, FoldList, ...


To explain my interest for seamless changes of baseforms

2 ArcCot[GoldenRatio^2^^1111] == ArcCot[2^^1010101010]
2^^1010101010 // BaseForm[#, 4] &



I tried myself another approch, but it has an disadvantage: HoldForm isn't respected

base[b_] := Function[# // ReplaceAll[#, BaseForm[x_, _] :> BaseForm[x, 10]] & // 
     ToString // ToExpression // BaseForm[#, b] &]

testsuite = {5555, BaseForm[5555, 8], HoldForm@Plus[5000, 555],5*BaseForm[1111, 2]}
base@10 /@ testsuite

You can define your own function that works with input the BaseForm of a number, sure:

myBase[a_?NumericQ, base_Integer] := BaseForm[a, base];
myBase[a_BaseForm, base_Integer] :=
    IntegerString @@ a,

so that

63969 // myBase[#, 16] & // myBase[#, 8] &

gives you BaseForm[63969,8] but using as input the number's representation in hexadecimal.

  • $\begingroup$ I often change the base of a number. Let's say I stored the number already in variable x. Now I don't want to remember if it's stored as a BaseForm or not. It shouldn't be important at all. Just using x//base@2, and that's it $\endgroup$ – hieron Aug 20 '14 at 15:42
  • $\begingroup$ thanks - so then it is just an issue of defining a function that works on BaseForm. What I didn't understand why you'd want to go through the process of jumping from base to base but if it is on potentially stored numbers it makes sense. $\endgroup$ – gpap Aug 20 '14 at 15:46
base = If[Head[#] === BaseForm, BaseForm[First @ #, #2], BaseForm[##]] &;
FoldList[base, 63696, {16, 8, 2}]

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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