# Thread: Decimal to binary, octal to decimal

1. Member Join Date
Dec 2010
Posts
14
Rep Power
0

## Decimal to binary, octal to decimal

Hello to all!

I am new on this forum, and I am quite new to Java too. I am from Slovenia, and I don't speak English very well, so please excuse me for mistakes.

Ok, for homework I have to write a program, which convert from decimal to binary and than another program, which convert from octal to decimal.
In the program which convert from decimal to binary, I have to use while loop, and this is a problem. I know how to do it with toBinaryString(), but I don't have idea, how to do it with While loop.

So, write this program, to convert from decimal to binary. The Binary number is save in variable a, but I have no Idea how to revese it. Now, the (Decimal) 55 = (Binary)111011, but it is wrong, it have to be 110111. How can I reverse this number?

Java Code:
```import java.util.*;

public class Convert
{

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.println("Input decimal number: ");
int dec = in.nextInt();

int udec = dec;
int ost;
String a = new String();

while (udec >= 1)
{
ost = udec % 2;

a = ost + "";
udec /=2;
System.out.print(a);
}

}
}```
And now, I have got another question. I have no Idea, how to convert from Octal to Decimal. In this case isn't necessarily to use While loop, I should use any of method, if they exists. :) But it is to complicated for me, so, if you have any idea, i will be happy.

Thanks to all!  Reply With Quote

2. ## Octal to decimal conversion is easy. Please have a look here: Octal - Wikipedia, the free encyclopedia

Also, Integer has a method, toOctalString and parse that will help. Your best way to learn these methods is to play with them with different numbers and Strings and see what happens.  Reply With Quote

3. Senior Member Join Date
Dec 2010
Posts
165
Rep Power
10

## decimal to binary
Java Code:
```import java.util.Scanner;
public class decToBinary {
public static void main(String[] args ){
StringBuilder str = new StringBuilder();
System.out.printf("Enter a decimal number: ");
int number=0;
Scanner input = new Scanner ( System.in );
number = input.nextInt();
while( number > 1){
number = number / 2;
str.append ( (number % 2 == 0) ? 0 : 1 );
}
str.insert(0,0);
System.out.println("str: " + str.reverse().toString() );
}
}```
octal to decimal
Java Code:
```import java.util.Scanner;
public class octToDecimal {
public static void main(String[] args ){
System.out.printf("Enter an octal number: ");
String number;
Scanner input = new Scanner ( System.in );
number = input.nextLine();

// store values from 1, 8, 64 ....
// if number input is 100 for example, then octals array will contain 1,8,64
int[] octals = new int[number.length() ];
octals=1;
for (int i = 1;i< number.length() ;i++){
octals[i] = octals[i-1] * 8;
}

int j = 0;
int sum=0; // add up each position
for (int i = number.length()-1 ; i>=0 ;i--){
// starting from end of the string
char c = number.charAt(i) ;
// accumulate the sum
sum += Character.digit(c,10) * octals[j] ;
j++;
}
System.out.println("Decimal value for "+ number + " is: " + sum );

}
}```  Reply With Quote

4. Member Join Date
Dec 2010
Posts
14
Rep Power
0

## Thanks to both of you :)
Fubarable, thanks for link. Now, I understand this convert :P Now, I have to play with all this metods, just you say.

JavaHater, woo, thanks for all. You made my dam brighter :D Really, really thanks for this. (I will not just copy-paste it to my homework, but I will try to understand it, so I hope, I will learn something new)

BTW, this forum is great  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•