×

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

Related Topics

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Character Array in Java

A character array is an array which holds values of character data types. It is different from a string array. The character array, string, and StringBuffer classes in the Java...

4 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

How to download and install Eclipse in Windows?

Download and Install Eclipse on Windows Eclipse is an open source IDE (Integrated Development Environment) which is used to help the programmers to provide a platform to write and run the...

1 minute read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.