Hexadecimal to Decimal Conversion in Java

Hexadecimal to Decimal Conversion

Here we will learn the concept of hexadecimal conversion to decimal also learn to code it in java.

Hexadecimal numbers range [ 0, 15 ]

  • [ 1, 9 ] same as integer
  • [ 10, 15 ] as [ ‘A’,  ‘F’ ] 
Java Program for hexadecimal to decimal conversion

Hexadecimal Working :

As discussed in hexadecimal numbers range [ 0, 15 ]. Where [ 0, 9 ] are represented same as integer values but after 9 alphabets are used as shown below :

  • A = 10
  • B = 11
  • C = 12
  • D = 13
  • E = 14
  • F = 15

How To Convert From Hexadecimal to Decimal Manually?

For a user input num. This requires you to know ASCII values, please check the ASCII table here 

To convert a hexadecimal to a decimal manually, you must start by multiplying the hex number by 16. Then, you raise it to a power of 0 and increase that power by 1 each time according to the hexadecimal number equivalent.

We start from the right of the hexadecimal number and go to the left when applying the powers. Each time you multiply a number by 16, the power of 16 increases.

When converting a C9 hexadecimal to a decimal your work should look something like this:

Example :

  • 9 = 9 * (16 ^ 0) = 9
  • C = 12 * (16 ^ 1) = 192

Then, we add the results.

  • 192 + 9 = 201 
Hexadecimal 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Java Code

Run
class Main
{
  public static void main (String[]args)
  {

    String hex = "C9";
    System.out.println (convert (hex));
  }
  
  static int convert(String hex){  
    String digits = "0123456789ABCDEF";  
             hex = hex.toUpperCase();  
             int val = 0;  
             for (int i = 0; i < hex.length(); i++)  
             {  
                 char c = hex.charAt(i);  
                 int d = digits.indexOf(c);  
                 val = 16*val + d;  
             }  
             return val;  
  }  

}

Output

Decimal value of C9 is 201

4 comments on “Hexadecimal to Decimal Conversion in Java”


  • pjanhabi584

    import java.util.*;
    public class Hexa_decimal
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println(“Enter a hexadecimal number : “);
    String hex=sc.nextLine();
    int decimal=Integer.parseInt(hex,16);
    System.out.println(“Decimal version is : “+decimal);
    }
    }


  • Kundan

    import java.util.*;
    class main{
    static String decimal (String hexa){
    String req = “123456789ABCDEF”;
    int decimal = 0;
    int j=0;
    for(int i=hexa.length()-1;i>=0;i–){
    decimal += (req.indexOf(hexa.charAt(i))+1)*Math.pow(16, j++);

    }
    return “HexaDecimal :”+hexa +” == “+”Decimal : “+decimal;
    }
    public static void main(String[] args) {
    String hexa = “C9”;
    System.out.println(decimal(hexa));
    }
    }


  • anurag

    package com.company;
    import java.util.*;
    class Main {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println(“enter the hexadecimal number”);
    String n=input.next();
    String digits = “0123456789ABCDEF”;
    n = n.toUpperCase();
    int l=n.length(),a=0,b=0;
    int sum=0;
    l=l-1;
    for(int i=0;i<=l;i++)
    {
    char ch=n.charAt(i);
    a=digits.indexOf(ch);
    sum=sum+(a*((int)Math.pow(16,l)));
    l–;

    }
    System.out.println(sum);
    }
    }


    • Tridip

      public class Hexadecimal {

      public static double convert(String hex){ //C9

      String digit =”0123456789ABCDEF”;

      hex = hex.toUpperCase();

      int v=hex.length();//2
      double sum =0;

      for(int i=0 ; i<hex.length();i++){

      char ch = hex.charAt(i);// C,9

      int d = digit.indexOf(ch);//C index =12 , 9 index =9

      sum = sum + (d * Math.pow(16, v-1));
      v–;

      }
      return sum;

      }

      public static void main(String[] args){

      System.out.println(convert("7cf"));

      }

      }