This discussion is archived
8 Replies Latest reply: Feb 6, 2008 6:38 AM by 807603

# Converting decimal to binary?

Currently Being Moderated
Hey, i'm trying to convert decimal to binary without using the actual function in java.

It's in a method that i will call in the main, but here is the beggining that i have; i need to assign the remainders into a string, but i don't get how to do it

public static String convertToBinary(int a)
{
String result;
while (a !=0)
{
a = a/2;
int rem;
rem = a%2;

result = ""+rem;
}
return result;

}

any help would be greatly appreciated!!
• ###### 1. Re: Converting decimal to binary?
Currently Being Moderated
this right here:
``result = ""+rem;``
you keep resetting the value of result, as opposed to appending to it. Are you sure this is what you want to do?
• ###### 2. Re: Converting decimal to binary?
Currently Being Moderated
without using the actual function in java.
Do you mean that you don't want to use Integer#toBinaryString()? Why not?
• ###### 3. Re: Converting decimal to binary?
Currently Being Moderated
Do you mean that you don't want to use Integer#toBinaryString()? Why not?
Requirements of the assignment, typically.

~
• ###### 4. Re: Converting decimal to binary?
Currently Being Moderated
This rather sounds like a homework assignment then.
• ###### 5. Re: Converting decimal to binary?
Currently Being Moderated
Try this
``result += ""+rem;``
and try to initialize your result String before you use it
• ###### 6. Re: Converting decimal to binary?
Currently Being Moderated
thanks guys; i think result += ""+rem;
is right...this is a class assignment, in which i get the general idea of how to convert (keep dividing the number by 0 and the remainders produce the binary number).

the only problem i don't understand is how to actually get these remainders to return them in the method; i know they exist, but i don't know how to capture them. my teacher suggested putting them into an empty string, which i don't understand; does result += ""+rem; put it into an empty string? how do i call this string after?\

thanks for the help!!!
• ###### 7. Re: Converting decimal to binary?
Currently Being Moderated
this is the updated code that i have right now:

public static String convertToBinary(int a)
{
String result = "";
while (a !=0)
{

a = a/2;
int rem;
rem = a%2;

result+= ""+rem;

}
return result;

}
• ###### 8. Re: Converting decimal to binary?
Currently Being Moderated
Thanks for the help guys! I got it, and for anyone else who may need help, this is the way to do it (except i still have to make 10-15 in hexidecimal read a-f). Thanks again!!

public class numconvert
{
public static boolean convertSomeMore()
{
int choice;
String[] choices = {"Convert Some Numbers!", "Quit"};
choice=JOptionPane.showOptionDialog
(null, "What do you want to do?", "Choices",
0, 3, null, choices, null);
if (choice==0)
return true;
else return false;
}
public static int enterNumber()
{
int num;
num = Integer.parseInt(JOptionPane.showInputDialog("Enter a " +
"number between 0 and 255"));

while (num>255 || num < 0)
{
num = Integer.parseInt(JOptionPane.showInputDialog("Enter a " +
"number between 0 and 255"));
}
return num;
}

public static String convertToBinary(int a)
{
int rem;
String result = "";
String reversed = "";
while (a !=0)
{
rem = a%2;
a = a/2;
result+= ""+rem;

}

for (int i=result.length()-1; i>=0; i--)
reversed+=result.charAt(i);
return reversed;

}
public static String convertToOctal(int a)
{
int rem;
String result = "";
String reversed = "";
while (a !=0)
{
rem = a%8;
a = a/8;
result+= ""+rem;

}

for (int i=result.length()-1; i>=0; i--)
reversed+=result.charAt(i);
return reversed;

}
{
int rem;
String result = "";
String reversed = "";
while (a !=0)
{
rem = a%16;
a = a/16;
result+= ""+rem;

}

for (int i=result.length()-1; i>=0; i--)
reversed+=result.charAt(i);
return reversed;

}

public static void main(String[] args)
{
// variable declarations
int decNum;
String binNum, octNum, hexNum;

//changeJOP();

while(convertSomeMore())
{
decNum=enterNumber();

binNum = convertToBinary(decNum);
System.out.println(binNum);
octNum = convertToOctal(decNum);
System.out.println(octNum);