# converting numbers with ML

This topic is 4551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am currently working on an assignment in SML, or Standard ML, specifically, the version "from New Jersey." Don't quite know what that means myself, but I assume that it was developed at a uni somewhere in New Jersey. Anyway, because this is an assignment, I'm obviously not looking for code, and since the work is in ML, it's more about the implementation in ML and learning the functional programming paradigm, not working out how to do every little thing ourselves. After that, let me tell you what I'm asking for: I currently need to figure out if there is a way to convert a number from one base to another, digit by digit. If you think you can help, then it would be appreciated. If you can't because it's an assignment, that's okay, I won't hold it against you. [smile]

##### Share on other sites
A number in base x can be represented using the formula:
d0*x^0 + d1*x^1 + ... + dn*x^n

Where the dx's are the digits (from least significant to most) and ^ is to the power of. This can be exploited to convert a string of a variable base to a number. To convert a number of base x to a string, simply remember that every digit can only go up to x, and that each successive digit increases in relation to x as you go up in significance.

##### Share on other sites
I think what the previous poster was getting at is: just divide by the base and the remainder is the next digit: ie base 10 to base 4:

100/4 = 25 r 0
25/4 = 6 r 1
6/4 = 1 r 2
1/4 = 0 r 1

so 1210 base 4 is 100 base 10. I'm almost postive sml has an atoi() function. Floating point will be a bit more involved, but there you just multiply the fractional part by the new base.

##### Share on other sites
Thanks guys, but the thing is (which I probably should have mentioned in more detail) the reason I wanted it digit by digit is because the assignment is to write a bunch of functions for infinite integers that can use many different bases internally for computation. I'm using an integer list (since I'm an absolute beginner - been using ML for less than a week) to hold the digit for each place in the number (eg. 215 in base 10 would be [2,1,5]). The internal base for computation could be anywhere from 2 to 215-1.

So it's quite possible that an integer would not be able to hold the whole value.

Edit:: so, when loading a value, we need to convert it from whatever base it is provided in, into the internal base.

[Edited by - Endar on July 29, 2006 6:06:46 PM]

##### Share on other sites
Not that many people will be here on sunday (sunday for me, at least), bump.

##### Share on other sites
I see. So what you really want are routines to do addition, multiplication, etc. given a number list of any base, correct? In that case, just think about how you do those things by hand and it should be fairly easy.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015499
×