Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
@Mr.Wizard This is more than enough, thanks! –  MultiVerb Feb 25 '13 at 16:33
    
Oh, I misunderstood the format. Thank you andre! Mr. Wizard also :) –  MultiVerb Feb 26 '13 at 5:15
add comment

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`
share|improve this answer
    
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. –  Mr.Wizard Feb 25 '13 at 16:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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