This discussion is archived
9 Replies Latest reply: Aug 27, 2007 2:43 AM by 807605

# need help in converting decimal, binary, octal, and hexadecimal numbers

Currently Being Moderated
I'm trying to make a program that can convert binary to decimal, decimal to binary, octal to hexadecimal, and hexadecimal to binary. It runs well, but I'm not getting the answers that I should be getting. I'll post the source code:

import java.io.*;
public class Compute {

public static void main (String[] args) {
int num;
int rem;
String input;
String number;

try{
System.out.println("Main Menu: \nPlease type the number of your choice: \n 1 - Binary to Decimal \n 2 - Decimal to Binary \n 3 - Octal to Hexadecimal \n 4 - Hexadecimal to Binary \n 5 - Quit");

case 1:
System.out.println("Enter a Binary Number: ");
num=Integer.parseInt(number);
num=num + 2;
rem=num/2;
System.out.println(rem);
break;
case 2:
System.out.println("Enter a Decimal Number: ");
num=Integer.parseInt(number);
while (num/2 != 0){
rem=num%2;
num=num/2;
if(num==0)
break;
System.out.println(num + " base 10 = " + rem + " base 2");
}
break;

case 3:
System.out.println("Enter an Octal Number: ");
num=Integer.parseInt(number);
num=num + 3;
rem=num/2;
System.out.println(rem);
break;

case 4:
num=Integer.parseInt(number);
num=num * 2;
rem=num-2;
System.out.println(rem);
break;

case 5:
System.out.println("Good-bye!");
System.exit(0);

}

}
catch (IOException x){
System.out.println("Error In Input");

}
catch (NumberFormatException e){
System.out.println("Error In Input. Please Try Again.");
}

}
}

Ignore cases 1, 3, and 4; I haven't figured out what to do to convert one to the other.

Anyway, the only case that's really working is case 2. However, when I run it, I get this:

Enter a Decimal number:
10
5 base 10 = 0 base 2
2 base 10 = 1 base 2
1 base 10 = 0 base 2

My main problems:
1) The answer is wrong; it should be 110 base 2 instead of 010 base 2.
2) The output should be 10 base 10 = 110 base 2, not three separate lines per computation.
3) The program exits after I execute this part. I need it to return to the main menu.
4) Say I put 'A' in the "Main Menu" or "Enter A Decimal Number" part. It's an exception, but the program exits immediately. Is it possible for it to return to the main menu?

Help would be greatly appreciated. :) Also, I would appreciate it if someone can tell me what to do for the other cases.
• ###### 1. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
For starters 10 decimal = 1010 binary not 110 or 010.

You algorithm and understanding are both wrong.
• ###### 2. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
Must have made some wrong computation in my brain. My problem still persists, though.
• ###### 3. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
One obvious problem is you have the print statement inside the while loop. That is why you are printing out multiple lines instead of one answer at the end.
• ###### 4. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
look at class java.lang.Integer there you find methods:
toHexString
toOctalString
toBinaryString

• ###### 5. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
Can't use that; we're not allowed to.
• ###### 6. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
Finally got the first case done. Second case is almost done, just with some problems Here is my code:
``````import java.io.*;
public class Ito {

public static void main (String[] args) {
int num;
int rem;
int counter;
int decimal;
int x=1;
int y= 10;
int ans=0;
int z=0;
int exp = 0;
String input;
String number;

try{
System.out.println("Main Menu: \nPlease type the number of your choice: \n 1 - Binary to Decimal \n 2 - Decimal to Octal \n 3 - Octal to Hexadecimal \n 4 - Hexadecimal to Binary \n 5 - Quit");

case 1:
System.out.println("Enter a Binary Number: ");
char[] binary = number.toCharArray();
for (counter=number.length()-1; counter >= 0; counter--){
char c = binary[counter]; // reads the char per the number counter
if (c == '1'){
decimal = 1;
}
else if (c == '0'){
decimal = 0;
}
else{
System.out.println("Input is not a Binary Number");
break;} //sets the char to either 0 or 1, or prints and error
answer = ans += (decimal * x);
x = x*2;
}
break;

case 2:
System.out.println("Enter a Decimal Number: ");
num=Integer.parseInt(number);
int[] octal = new int[10];
for (counter=number.length()-1; counter >= 0; counter--){
rem = num % 8;
num = num/8;
octal[z] = rem;
z++;
}
System.out.println("the " + octal[1] + octal[0]);
break;

case 3:
System.out.println("Enter an Octal Number: ");
num=Integer.parseInt(number);
num=num + 3;
rem=num/2;
System.out.println(rem);
break;

case 4:
num=Integer.parseInt(number);
num=num * 2;
rem=num-2;
System.out.println(rem);
break;

case 5:
System.out.println("Good-bye!");
System.exit(0);

}
}
catch (IOException LOL){
System.out.println("Error In Input");

}
catch (NumberFormatException e){
System.out.println("Error In Input. Please Try Again.");
}

}
}``````
Look only at case 2.

Problems:
1. I had to declare a specific number of elements for the array, because if I don't, it tells me that the "variable octal might not have been initialized". Is is possible for the array to have an indefinite amount of elements?

2. Since I don't know how many elements my array will have, how can I print all of the elements in one straight line?

3. Please confirm what if there is something wrong in my for-loop. I'm pretty sure there is, I think the number.length part.
• ###### 7. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
Skip that last post, I've done it. Here is my new problem:

I can convert an octal number to binary, but I don't know how to turn the binary to a hexadecimal. How can I get four characters at a time?

Ex:
123 in Octal = 1010011 in binary

How do I take "0101" so that I can set it to "5" first, and then get "0011" to set it to "3"?
• ###### 8. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
If you are using Strings, look at the API for that class. It will tell you how to get the string length and how to get individual characters at specific locations. That will solve your problem.
• ###### 9. Re: need help in converting decimal, binary, octal, and hexadecimal numbers
Currently Being Moderated
I tried a different approach; I converted the octal to decimal first, then the decimal to hexadecimal. The first part is ok, but the dec -> hex gives me a wrong answer. EDIT: I got it now, but the answer is backwards!
``````case 3:
System.out.println("Enter an Octal Number: ");
num=Integer.parseInt(number);
char[] octalArray = number.toCharArray();
convertloop:
for (counter=number.length()-1; counter >= 0; counter--){
char c = octalArray[counter]; // reads each digit one by one
if (c == '0'){
decimal = 0;
}
else if (c == '1'){
decimal = 1;
}
else if (c == '2'){
decimal = 2;
}
else if (c == '3'){
decimal = 3;
}
else if (c == '4'){
decimal = 4;
}
else if (c == '5'){
decimal = 5;
}
else if (c == '6'){
decimal = 6;
}
else if (c == '7'){
decimal = 7;
}
else{
System.out.println("Input is not an Octal Number");
break convertloop;}
dec += (decimal * x);
x = x*8;
}   System.out.println(dec);

// This is the wrong part
while (dec != 0){
rem = dec % 16;
dec = dec/16;
ans = rem*y;
y = y * 10;
lol = String.valueOf(ans);
if (lol.equals("10"))
lol = "A";
else if (lol.equals("11"))
lol = "B";
else if (lol.equals("12"))
lol = "C";
else if (lol.equals("13"))
lol = "D";
else if (lol.equals("14"))
lol = "E";
else if (lol.equals("15"))
lol = "F";
else
lol = lol;