Results 1 to 4 of 4
  1. #1
    matejm1994 is offline Member
    Join Date
    Dec 2010
    Posts
    14
    Rep Power
    0

    Default 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    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[0]=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 );
    
            }
    }

  4. #4
    matejm1994 is offline Member
    Join Date
    Dec 2010
    Posts
    14
    Rep Power
    0

    Default

    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

Similar Threads

  1. Convert Decimal To Binary
    By aspire007 in forum New To Java
    Replies: 8
    Last Post: 08-06-2010, 07:32 AM
  2. Binary to Decimal Converter
    By c_walker in forum New To Java
    Replies: 15
    Last Post: 11-24-2009, 02:38 PM
  3. Replies: 4
    Last Post: 10-26-2009, 10:17 PM
  4. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:32 PM
  5. Eclipse- Decimal to binary
    By queen_vee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 02:17 PM

Posting Permissions

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