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?


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

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

You could do something like this:

toBaseString[n_?NumericQ, b_Integer?Positive] :=
  First @ StringSplit @ ToString @ BaseForm[n, b]

toBaseString[365.7, 5]

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
  • $\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 Feb 25 '13 at 16:38

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.