8
$\begingroup$

Say I want to convert some number $N$ to base $k$. Here, I might use the command BaseForm[N,`k], e.g., BaseForm[10,`2] which gives the output $1010_2$ (where 2 is subscript). How can I drop the subscript and manipulate the number as if it is a normal integer in decimal form? I'd ideally like to do this without any string processing.

$\endgroup$
0

2 Answers 2

10
$\begingroup$

Don't use BaseForm[number,base]

Use IntegerDigits[number,base] :

IntegerDigits[10, 2]

(* --->  {1, 0, 1, 0} *)

It returns a List of Integers, which is a very good thing for further processing

$\endgroup$
2
  • $\begingroup$ @Mr.Wizard This is more than enough, thanks! $\endgroup$
    – MultiVerb
    Commented Feb 25, 2013 at 16:33
  • $\begingroup$ Oh, I misunderstood the format. Thank you andre! Mr. Wizard also :) $\endgroup$
    – MultiVerb
    Commented Feb 26, 2013 at 5:15
5
$\begingroup$

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, 5] // InputForm
2430.322222222222`
$\endgroup$
2
  • $\begingroup$ It seems I was way off the mark but I'll leave this answer for anyone who finds this question through a search and may want something else. $\endgroup$
    – Mr.Wizard
    Commented Feb 25, 2013 at 16:38
  • $\begingroup$ Glad you kept the answer. I needed the toBaseString function that you wrote :) $\endgroup$ Commented Apr 6, 2021 at 13:18

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.