Hexadecimal to Decimal conversion in Java

This tutorial would help in learning the following:

  • Conversion of numbers from hexadecimal-system (0 to 9 and A to F) to decimal-system (using 0 to 9)
  • Re-conversion of characters into their respective ASCII codes
  • Code for Hexadecimal to Decimal conversion in Java

The decimal number system uses all the 10 digits (0 to 9) to represent a number while the hexadecimal uses 15 characters (0 to 9 and A to F, indicating 10 to 15 respectively). The following algorithm is used to convert a hexadecimal-number to a decimal-number:

let the number be 3F

  1. Give proper index to each digit (or character) of the hexadecimal number, starting with zero and from the rightmost digit (or character)
    number->3F
    index—->10
  2. multiply the number (or character with equivalent numerical value) present at index ‘i’ with the ith power of  16
    index 0 -> F * 160 = 15 * 160 = 15
    index 1 -> 3 * 161 = 48
  3. add all the products obtained from step 2, to get the equivalent decimal number
    15 + 48 = 63
    (3F)16 ~ (63)10

Each character used in computing is assigned a special ASCII code (decimal-number-system); hence, once a hexadecimal code is converted to its equivalent decimal-number code, the latter can be assigned a character as per the ASCII convention.
Note, there can be different encoding systems on different machines.

Java Code for Hexadecimal to Decimal conversion

Below is our program:

//This program follows the convention of giving a fullstop after the code for an individual character
public class HexaDeconversion
{
String hexacode;
HexaDeconversion()//default constructor
{
    hexacode="43.6F.64.65.20.53.70.65.65.64.79.";
}
HexaDeconversion(String x)//parameterized constructor
{
    hexacode=x;
}
public static void main(String args[])
 {
     HexaDeconversion ob;
    try{//considering the command line argument of the user as the string to be re-converted from hexadecimal format
        ob=new HexaDeconversion(args[0]);
    }//going with the default string in case of any exceptions with the command line argument
     catch(Exception e){ob=new HexaDeconversion();};
     System.out.print("The decoded value of hexadecimal code "+ob.hexacode+" is: ");
     ob.hexaToString(ob.hexacode);
 }
char hexa(char array[])
{
    int code=0;
    int temporary;
    int i;
    int j=0;
    for(i=0;array[i]!='\0';i++);//obtaining the index of the last code-character in array[]
    for(--i;i>=0;i--){
        if(Character.isLetter(array[i]))temporary = (int)array[i] - (int)'A' + 10;
        else temporary = ((int)array[i]) - ((int)('0'));
        code+=temporary*(int)(Math.pow(16,j++));//Math.pow() helps in giving the required power of 16
    }
    return (char)code;//returning the character corresponding to the integer-code whose value is in 'code'
}
void hexaToString(String code)
{
    char helper[]=new char[10+1];//assuming that the length of hexadecimal code for one character will not exceed 10
    int index = 0;
    for(int i=0;i<code.length();i++){
        if(code.charAt(i)=='.')//printing the value of the code for one character
        {
            helper[index]='\0';
            System.out.print(hexa(helper));
            index=0;
        }
        else//stacking up the incomplete code for one character
        {
            helper[index++]=code.charAt(i);
        }
    }
}
}//class ends

The output (without command line argument) of our program is given below:

The decoded value of hexadecimal code 43.6F.64.65.20.53.70.65.65.64.79. is: Code Speedy

Summary:

char hexa(char[]):

  • the index of the last character in array[] is stored in ‘i’
  • the value of ‘j’ corresponds with the required power of 16 that is required for the character at given index ‘i’
  • the numerical value of the character-code at index ‘i’ is stored in ‘temporary’
  • the sum of products for different indices is stored in ‘code’
  • the character corresponding to the value of ‘code’ is returned

void hexaToString(String):

  • helper[] stores the hexadecimal code for one character at a time
  • hexa(char[]) is called and helper[] is passed to it to print the corresponding character for the hexadecimal code in helper[]

If you want to know how to convert decimal to hexadecimal, then Decimal to Hexadecimal conversion is also here for you. I hope, you enjoyed this article.

Leave a Reply

Your email address will not be published. Required fields are marked *