Java Program to convert int to Octal String

Java 8Object Oriented ProgrammingProgramming

The Integer.toOctalString() method in Java converts int to octal string.

Let’s say the following are our integer values.

int val1 = 9;
int val2 = 20;
int val3 = 2;

Convert the above int values to octal string.

Integer.toOctalString(val1);
Integer.toOctalString(val2);
Integer.toOctalString(val3);

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      int val1 = 9;
      int val2 = 20;
      int val3 = 30;
      int val4 = 78;
      int val5 = 2;
      System.out.println("Converting integer "+val1+" to Octal String:       "+Integer.toOctalString(val1));
      System.out.println("Converting integer "+val2+" to Octal String: "+Integer.toOctalString(val2));
      System.out.println("Converting integer "+val3+" to Octal String: "+Integer.toOctalString(val3));
      System.out.println("Converting integer "+val4+" to Octal String: "+Integer.toOctalString(val4));
      System.out.println("Converting integer "+val5+" to Octal String: "+Integer.toOctalString(val5));
   }
}

Output

Converting integer 9 to Octal String: 11
Converting integer 20 to Octal String: 24
Converting integer 30 to Octal String: 36
Converting integer 78 to Octal String: 116
Converting integer 2 to Octal String: 2
raja
Published on 13-Dec-2018 09:38:24
Advertisements