binary to octal in java

Solutions on MaxInterview for binary to octal in java by the best coders in the world

showing results for - "binary to octal in java"
Louisa
04 Jun 2017
1public class UsingtoOctalStringMethod
2{
3   public static void main(String[] args)
4   {
5      String strNumber = "100101";
6      int binary = Integer.parseInt(strNumber, 2);
7      String strOctal = Integer.toOctalString(binary);
8      System.out.println("Octal value is: " + strOctal);
9   }
10}
Fabiana
05 Sep 2020
1public class BinaryToOctal
2{
3   public static void main(String[] args)
4   {
5      long binaryNumber = 1010111;
6      int octalNumber = convertToOctal(binaryNumber);
7      System.out.println(binaryNumber + " in binary is equal to " + octalNumber + " in octal.");
8   }
9   public static int convertToOctal(long binaryNumber)
10   {
11      int octal = 0, decimal = 0, a = 0;
12      while(binaryNumber != 0)
13      {
14         decimal += (binaryNumber % 10) * Math.pow(2, a);
15         ++a;
16         binaryNumber /= 10;
17      }
18      a = 1;
19      while(decimal != 0)
20      {
21         octal += (decimal % 8) * a;
22         decimal /= 8;
23         a *= 10;
24      }
25      return octal;
26   }
27}
Lucia
10 Sep 2018
1import java.util.Scanner;
2public class OctalToBinaryJava
3{
4   public static void main(String[] args)
5   {
6      Scanner sc = new Scanner(System.in);
7      System.out.println("Please enter octal number: ");
8      int octal = Integer.parseInt(sc.nextLine(), 8);
9      String strBinary = Integer.toBinaryString(octal);
10      System.out.println("Binary value is: " + strBinary);
11      sc.close();
12   }
13}
Melina
19 Jun 2020
1// conversion of binary to octal in java..
2public class Main{
3    public static void main(String args[]) throws Exception{
4        String binary = "1100100";                // binary number.. 
5        int decimal = Integer.parseInt(binary,2); // converting binary to decimal
6        System.out.println(Integer.toOctalString(decimal)); // 144 <= octal output..  
7    }
8}