Results 1 to 4 of 4
 12262010, 12:33 AM #1Member
 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); } } }
Thanks to all!

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.
 12262010, 06:54 AM #3Senior Member
 Join Date
 Dec 2010
 Posts
 165
 Rep Power
 8
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() ); } }
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[0]=1; for (int i = 1;i< number.length() ;i++){ octals[i] = octals[i1] * 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 ); } }
 12262010, 10:59 AM #4Member
 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 copypaste it to my homework, but I will try to understand it, so I hope, I will learn something new)
BTW, this forum is great
Similar Threads

Convert Decimal To Binary
By aspire007 in forum New To JavaReplies: 8Last Post: 08062010, 08:32 AM 
Binary to Decimal Converter
By c_walker in forum New To JavaReplies: 15Last Post: 11242009, 03:38 PM 
how to convert decimal value into 8bit binary value
By tOpach in forum New To JavaReplies: 4Last Post: 10262009, 11:17 PM 
Convert binary into decimal
By WarmRegards in forum New To JavaReplies: 8Last Post: 10182009, 03:32 PM 
Eclipse Decimal to binary
By queen_vee in forum New To JavaReplies: 1Last Post: 02242009, 03:17 PM
Bookmarks