octal to binary in java

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

showing results for - "octal to binary in java"
Giuseppe
25 Jun 2018
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}
Amin
17 Jan 2021
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}
Emilio
01 Oct 2017
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}