×

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 Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.