Convert number to given base (JAVA)

Rakada -  
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

1 answer

KX Posted messages 19031 Status Moderator 3 020
 
"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:

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
0
Rakada
 
"\"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 ... )))\"

What does this equality mean?"
0
KX Posted messages 19031 Status Moderator 3 020
 
It is the decomposition of a number into its digits. Example in decimal: 1234 = 4 + 3*10 + 2*100 + 1*1000 = 4 + 10 * (3 + 10 * (2 + 10 * (1))) This is how the decomposition by successive division and remainders is derived. [ 4 + 10 * ( 3 + 10 * ( 2 + 10 * (1 ))) ] / 10 = ( 3 + 10 * ( 2 + 10 * (1) )) remainder 4 [ 3 + 10 * ( 2 + 10 * (1 )) ] / 10 = ( 2 + 10 * (1 )) remainder 3 [ 2 + 10 * (1 ) ] / 10 = ( 1 ) remainder 2 [ 1 ] / 10 = 0 remainder 1 We indeed recover the digits 1, 2, 3, 4 of the decomposition of 1234 in base 10. You can do the same in any base: by dividing N by b you get remainder b0 and the quotient N' = b1 + b.( b2 + b.( b3 ... )) which allows to calculate b1, b2, b3, ... Example in hexadecimal: 1234 / 16 = 77 remainder 2 77 / 16 = 4 remainder 13 4 / 16 = 0 remainder 4 Thus 1234 = 2 + 16 * (13 + 16 * (4)) = 2 + 16 * 13 + 16² * 4 The hexadecimal digits of 1234 are therefore 4, 13, 2, which is represented as "4d2"
0
Rakada
 
Here is the translation: Here I go! this gives me this for the value 1234 in base 16:

void main() {
println("Enter a number:");
int N = readInteger();
println("Enter a base:");
int b = readInteger();
int r0 = N / b;
int b0 = N % b;
int r1 = r0 / b;
int b1 = r0 % b;
int r2 = r1 / b;
int b2 = r1 % b;
String hex2 = Integer.toHexString(b2);
String hex1 = Integer.toHexString(b1);
String hex = Integer.toHexString(b0);
println(hex2+""+hex1+""+hex);
}

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 ;)
0
KX Posted messages 19031 Status Moderator 3 020
 
I think you should avoid using the Integer.toHexString method; it only applies to base 16, but if you take b=17, 18... it won’t work anymore. Moreover, it’s "cheating" — the goal of your program is to do everything yourself; otherwise you would directly use the Integer.toString(n,b) method which already does everything. "i’d like to add b3 and r3" Indeed, especially since if you choose small bases, you’ll have many digits. For example, 1234 gives "10011010010" in base 2... if you only keep b0, b1, b2 you couldn’t go beyond n=7 for base 2, which is frankly very little! Therefore you need to use a loop, so that you have as many b/r as necessary without having to code them all yourself one after the other.
0