Select Page

How to Convert Decimal to Octal in Java

There are two methods to convert Decimal to Octal:

• Using toOcatlString() method
• Using user-defined logic

Using Integer.toOctalString() method

The toOctalString() is astaticmethod of the Integer wrapper class that converts decimal to an octal string. The signature of the method is given below:

public static String toOctalString(int decimal)

Example

In the following example, we have invoked the toOctalString() method of Integer wrapper class. The method parses an integer argument which you want to convert and returns the octal representation of the decimal number.

Output

Using user-defined logic

You can also convert decimal to octal in using user-defined logic.

Example

In the following example, we have defined a method of name conversion. Inside the method body, we have taken two variables rem (represent remainder) of type integer, and octal (represent octal number) of type String. We have also taken an array octalchar[]of characters, and assign values from ‘0’to ‘7’ into it.

The method has a while loop that checks the condition and performs the iterations. Let’s see how the while loop will be executed.

Remember: The second statement of the while loop performs concatenation instead of addition.If you change the sequence of the statement octal=octal+octalchars[rem]then the output will be 41 which is the wrong output. So remember the sequence while you write this statement.

For first iteration

decimal=12                                                                                         //passed parameter

12>0                                                                                                        //condition true

rem=12%8 = 4                                                                                   //finds remainder

octal=octalchars[4]+octal = 4                                      //updated value of octal is 4

decimal=12/8 = 1                                                              //updated value of decimal is 1

For second iteration

decimal=1

1>0                                                                                                           //conditiontrue

rem=1%8 = 1                                                                                      //finds remainder

octal=octalchars[1]+octal = 14                                    //updated value of octal is 4

decimal=1/8 = 0                                                                                //updated value of decimal is 0

For third iteration

decimal=0

0>0                                                                                                         //condition false

The while loop terminates the execution, and returns the converted octal value. Similarly you can also perform iteration for the decimal value 75.

Hence the third iteration will not be executed. The while loop terminates the execution. The method conversion returns the octalvalue.

Output