Octal to Binary in java

Let’s learn octal to binary in java.

Octal to Binary in java

An octal number is base 8 number system which uses digits from 0 to 7. Binary number is expressed in base 2 binary numercal system which uses two digits 0 and 1.

In the below java program first we get input from user using nextLine() method of Scanner class. Meanwhile this user entered octal number is parsed using Integer.parseInt(String s, int radix) method and then stored to integer variable ‘octal’.

Integer.parseInt(String s, int radix) method returns the integer represented by the string argument in the specified radix.

In the next step this integer variable ‘octal’ value is passed as an argument to Integer.toBinaryString() method. This method returns the string representation of the unsigned integer value represented by the argument in binary (base 2).

Finally print the binary value on the console. Here’s the program on octal to binary.

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);


Please enter octal number:
Binary value is: 1100111

Also read – comments in java