# How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal:

• Using parseInt() method
• Using user-defined logic
Using Integer.parseInt() method The Integer.parseInt() method is a static method of Integer wrapper class. It converts String to an integer with the given radix. It parses two arguments: stringstr which you want to convert into octaland a radix.The signature of the method is given below:
`public static int parseInt(String str,int radix)`
Example In the following example, we have taken a variable octalstr of type String and assign"323" into the variable.Integer class invokes the parseInt() method that returns the decimal representation of the octal string.The method parses two arguments: one is octalstr which you want to convert into decimal and the second is the radix of octal number.The println statement prints the decimal value of the octal number.
```public class OctalToDecimalExample
{
public static void main(String args[])
{
String octalstr="323";
int decimal=Integer.parseInt(octalstr,8);
System.out.println("The decimal equivalent of 323 is:"+decimal);
}
}```
Output The decimal equivalent of 323 is:211 Using user-defined logic You can also convert octal into decimal using user-defined logic. Example In the following example, we have defined a static method conversionand its return type is an integer. It parses anint parameter. The method has a while loop that tests the condition and performs the iteration. The while loop has three statements. Inside the while loop, Math is a class and pow() is the method of Math class. It parses two arguments: base and power. It returns the value of the first argument raised to the power of pwr. Let’s see how while loop will execute. For first iteration octal=455, decimalNumber=0, pwr=0                                     //initialized values while(455!=0)                                                                                    //condition true decimalNumber=decimalNumber+(455%10)*Math.pow(8,0) decimalNumber =0+5*80=5                                                         //update value of decimalNumber is 5 ++pwr                                                                                                   //increments pwr by 1 i.e. pwr=1 octal=455/10 = 45                                                                             //update value of octal is 45 For second iteration octal=45, decimalNumber=5, pwr=1 while(45!=0)                                                                                      //condition true decimalNumber=decimalNumber+(45%10)*Math.pow(8,1) decimalNumber=5+5*81=45                                                       //update value of decimalNumber is 45 ++pwr                                                                                                   //increments pwr by 1 i.e. pwr=2 octal=45/10 = 4                                                                                 //update value of octal is 4 For third iteration octal=4, decimalNumber=45, pwr=2 while(4!=0)                                                                                         //condition true decimalNumber=decimalNumber+(4%10)*Math.pow(8,2) decimalNumber=45+4*82=301                                                   //update value of decimalNumber is 301 ++pwr                                                                                                   //increments pwr by 1 i.e. pwr=3 octal=0/10 = 0                                                                                    //update value of octal is 0 For fourth iteration octal=0, decimalNumber=301, pwr=3 while(0!=0)                                                                         //condition false Hence the fourth iteration will not be execute. The while loop terminate the execution. The method conversion will return the converted decimal value to the calling function in the main method. The println statement prints the decimal representation of specified octal value i.e. 301.
```public class OctalToDecimalExample1
{
public static void main(String[] args)
{
int octal=455;
int decimal = conversion(octal);                                                 //function calling
System.out.println("The decimal equivalent of 455 is:"+decimal);
}
public static int conversion(int octal)                                       //called function
{
int decimalNumber=0, pwr=0;
while(octal!= 0)                                                                                                //checks condition
{
decimalNumber=decimalNumber+(octal % 10)*Math.pow(8, pwr);
++pwr;                                                                                                 //increments value of pwr
octal=octal/10;
}
return decimalNumber;
}
}```
Output
`The decimal equivalent of 455 is:301`