Convert number to given base (JAVA)
Rakada
-
KX Posted messages 19031 Status Moderator -
KX Posted messages 19031 Status Moderator -
Hello,
The title isn’t very clear I’ll admit, but I wanted to keep it simple and short! Anyway, I’m in my final year and I’m working on a small project where I’m getting stuck a lot. The goal is to convert a given number N from decimal representation to a base b chosen by the user. The base b must be between 2 and 36 inclusive. I thought about using modulo to get the remainder and division to get the digit from the modulo, but it’s a bit of a puzzle! So I’d have liked some ideas on how to achieve my aim!
Thanks in advance.
PS: I should mention that I code with Java using the Java's cool software.
Configuration: Windows 7 / Chrome 24.0.1312.57
The title isn’t very clear I’ll admit, but I wanted to keep it simple and short! Anyway, I’m in my final year and I’m working on a small project where I’m getting stuck a lot. The goal is to convert a given number N from decimal representation to a base b chosen by the user. The base b must be between 2 and 36 inclusive. I thought about using modulo to get the remainder and division to get the digit from the modulo, but it’s a bit of a puzzle! So I’d have liked some ideas on how to achieve my aim!
Thanks in advance.
PS: I should mention that I code with Java using the Java's cool software.
Configuration: Windows 7 / Chrome 24.0.1312.57
1 answer
"from decimal representation to an arbitrary base b"
Be careful with terminology: if it's a base 'b' it is not decimal (unless b=10...)
"I thought of making a modulo to get the remainder and a division to get the digit of the modulo"
The idea is good, that's how you should do it.
"The base b must be between 2 and 36 inclusive"
These are not arbitrary values; Java also handles this range, it allows manipulating an alphabet with 10 digits and 26 letters.
Reminder:
--
Confidence does not exclude verification
Be careful with terminology: if it's a base 'b' it is not decimal (unless b=10...)
"I thought of making a modulo to get the remainder and a division to get the digit of the modulo"
The idea is good, that's how you should do it.
"The base b must be between 2 and 36 inclusive"
These are not arbitrary values; Java also handles this range, it allows manipulating an alphabet with 10 digits and 26 letters.
Reminder:
N = n0 + 10.n1 + 100.n2 + 1000.n3... = n0 + 10.( n1 + 10.( n2 + 10.( n3... )))
= b0 + b.b1 + b².b2 + b³.b3... = b0 + b.( b1 + b.( b2 + b.( b3 ... )))
--
Confidence does not exclude verification
= b0 + b.b1 + b².b2 + b³.b3... = b0 + b.( b1 + b.( b2 + b.( b3 ... )))\"
What does this equality mean?"
Afterwards I don’t know if I should repeat this for every digit entered, I’d like to do something simpler for any number because the program I made works only for a 4-digit number no more. To do more I’d have to add b3 and r3, etc but I think there must be a simpler way but I don’t see it :/ .
PS: the result obtained when typing 1234 in base 16 is indeed 4d2 ;)