binary to octal in java

public class UsingtoOctalStringMethod
{
   public static void main(String[] args)
   {
      String strNumber = "100101";
      int binary = Integer.parseInt(strNumber, 2);
      String strOctal = Integer.toOctalString(binary);
      System.out.println("Octal value is: " + strOctal);
   }
}

4
7
David 105 points

                                    import java.util.Scanner;
public class OctalToBinaryJava
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter octal number: ");
      int octal = Integer.parseInt(sc.nextLine(), 8);
      String strBinary = Integer.toBinaryString(octal);
      System.out.println("Binary value is: " + strBinary);
      sc.close();
   }
}

4 (7 Votes)
0
4.13
8
Taranh 90 points

                                    // conversion of binary to octal in java..
public class Main{
    public static void main(String args[]) throws Exception{
        String binary = "1100100";                // binary number.. 
        int decimal = Integer.parseInt(binary,2); // converting binary to decimal
        System.out.println(Integer.toOctalString(decimal)); // 144 <= octal output..  
    }
}

4.13 (8 Votes)
0
4
1

                                    public class BinaryToOctal
{
   public static void main(String[] args)
   {
      long binaryNumber = 1010111;
      int octalNumber = convertToOctal(binaryNumber);
      System.out.println(binaryNumber + " in binary is equal to " + octalNumber + " in octal.");
   }
   public static int convertToOctal(long binaryNumber)
   {
      int octal = 0, decimal = 0, a = 0;
      while(binaryNumber != 0)
      {
         decimal += (binaryNumber % 10) * Math.pow(2, a);
         ++a;
         binaryNumber /= 10;
      }
      a = 1;
      while(decimal != 0)
      {
         octal += (decimal % 8) * a;
         decimal /= 8;
         a *= 10;
      }
      return octal;
   }
}

4 (1 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source