Tuesday, June 7, 2022

[Solved] How to convert Hexadecimal to Decimal, Binary and Octal in Java? Example

Hexadecimal to decimal and binary conversion in Java
Hello guys, if you are wondering how to convert a Hexadecimal number to decimal and binary number in Java then you have come to the right place. Earlier, I have shown you how to convert decimal to binary in Java and in this Java program will convert Hexadecimal number to decimal, binary and Octal in Java programming language using JDK standard API methods. For beginners who are not familiar with number system, hexadecimal system is base 16 number, while decimal is base 10, Octal is base 8, and binary is base 2 numbers in Number systems. 

Binary only contains 0 and 1 bits, octal contains 0 to 7, decimal contains 0 to 9 and Hexadecimal contains 0-9,A,B,C,D,E,F where F represent 15. Thankfully Java library provides a convenient method to convert any integer from one number system to another. 

In our last article we have seen How to convert decimal numbers to binary in Java and in this article we will convert Hexadecimal numbers to binary, octal, and decimal numbers.

If you are going for programming interviews, then it's better to prepare How to do this exercise without using Java API as well, as the interviewer commonly expects a programmer to create there own method during interviews like reversing String without using StringBuffer reverse() method.



Convert Hexadecimal to Decimal, Binary, and Octal in Java

Java API provides two methods that are used in converting a number from one number system to other. One is Integer.parseInt() which is used to convert String to Integer in Java but also allows you to specify radix or base. This means by using parseInt() you can convert any Hexadecimal String to decimal number in Java. 

The same technique can be used to convert binary String or Octal String to decimal numbers in Java. Once you got an Integer object representing Decimal number you can use Integer.toBinaryString() and Integer.toOctalString() to convert same Hexadecimal number into Binary and Octal in Java. 




here is a complete code example of converting Hexadecimal number to Binary, decimal, and Octal in Java.

/**
 *
 * Java program to convert Hexadecimal to binary, decimal, and Octal in Java.
 * Hexadecimal is base 16, the Decimal number is base 10, Octal is base 8
 * and Binary is base 2 number which has just two numbers 0 and 1.
 * @author
 */

public class ConvertHexaToDecimal {

 
    public static void main(String args[]) {
        // Ask user to enter an Hexadecimal number in Console
        System.out.println("Please enter Hexadecimal number : ");
        Scanner scanner = new Scanner(System.in);
     
        String hexadecimal = scanner.next();
     
        //Converting Hexa decimal number to Decimal in Java
        int decimal = Integer.parseInt(hexadecimal, 16);
     
        System.out.println("Converted Decimal number is : " + decimal);
   
        //Converting hexadecimal number to binary in Java      
        String binary = Integer.toBinaryString(decimal);
        System.out.printf("Hexadecimal to Binary conversion of %s is %s %n", hexadecimal, binary );
     
        // Converting Hex String to Octal in Java
        String octal = Integer.toOctalString(decimal);
        System.out.printf("Hexadecimal to Octal conversion of %s is %s %n", hexadecimal, octal );
    }
}

Output:
Please enter Hexadecimal number :
A
Converted Decimal number is : 10
Hexadecimal to Binary conversion of A is 1010
Hexadecimal to Octal conversion of A is 12


Here is also a complete chart of converting 1 to 20 numbers from decimal to binary to octal to hexadecimal. You should also try to remember these values, it will help a lot. 

How to convert Hexadecimal to Decimal, Binary and Octal in Java program - Example



An advantage of using Java API for Hexadecimal to Decimal, Binary and Octal conversion is that, Above code can also convert negative Hexadecimal number to binary, octal and decimal in Java as shown in below Output:

Please enter Hexadecimal number :
-B
Converted Decimal number is : -11
Hexadecimal to Binary conversion of -B is 11111111111111111111111111110101
Hexadecimal to Octal conversion of -B is 37777777765

Hexadecimal to decimal, Hexadecimal to binary and Hexadecimal to Octal in JavaThat's all on How to convert Hexadecimal number to Decimal in Java. By using a similar technique we can also convert Hex numbers to binary and Octal as well. Let us know if you come across any other way to convert hex numbers to binary, decimal, and Octal in Java.


Related Data Structure and Algorithm Interview Questions from Javarevisited Blog
  • How to reverse an array in place in Java? (solution)
  • 50+ Data Structure and Algorithms Problems from Interviews (questions)
  • How to remove duplicate elements from an array in Java? (solution)
  • How to check if a number is binary in Java? (answer)
  • Top 30 Array Coding Interview Questions with Answers (see here)
  • Top 15 Data Structure and Algorithm Interview Questions (see here)
  • How to find a missing number in an array? (answer)
  • How to find all pairs whose sum is equal to a given number in Java (solution)
  • Top 30 linked list coding interview questions (see here)
  • Write a Program to remove duplicates from an array without using Collection API? (program)
  • How to reverse String in Java without using API methods? (Solution)
  • Write a method to remove duplicates from ArrayList in Java? (Solution)
  • Top 50 Java Programs from Coding Interviews (see here)
  • 5 Free Data Structure and Algorithms Courses for Programmers (courses)
  • How to compare two arrays in Java? (answer)
  • Top 20 String coding interview questions (see here)
  • 10 Algorithms Books Every Programmer Should Read (books)
  • 10 Free Data Structure and Algorithm Courses for Programmers (courses)
  • 100+ Data Structure Coding Problems from Interviews (questions)

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or doubt then please let us know and I'll try to find an answer for you. As always suggestions, comments, innovative and better answers are most welcome.

P. S. - If you are looking for some Free Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check the Data Structure in Java free course on Udemy. It's completely free and all you need to do is create a free Udemy account to enroll in this course. 

9 comments:

  1. A very good tutorial for beginner like me. Thanks a lot !

    ReplyDelete
  2. great simple and easy thx

    ReplyDelete
  3. Thank you so much, I've been struggling with this for quite a while. :)

    ReplyDelete
    Replies
    1. Hello @Annoymous, glad that you find this code of hexadecimal to binary and decimal conversion useful.

      Delete
  4. Please enter Hexadecimal number :
    Ef557E45
    Exception in thread "main" java.lang.NumberFormatException: For input string: "Ef557E45"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at ConvertHexaToDecimal.main(ConvertHexaToDecimal.java:14)

    ReplyDelete
  5. Beginning paragraph, "9,A,B,C,D,E,F where F represent 16.." is false. Hex is 0-15, therefore F is actually 15 not 16.

    ReplyDelete
    Replies
    1. Yes, that's correct F=15 in Hexadecimal, thanks for pointing out.

      Delete
  6. But it doesn't print out any leading 0's

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.