Convert hexadecimal to decimal – java api, iterative & recursive

  • Given a hexadecimal number, convert hexadecimal number to decimal number using java.
  • We will discuss various methods to convert hexadecimal number to decimal.
    • Convert hexadecimal to decimal using Integer class
      • Integer.parseInt
    • Iterative program to convert hexadecimal number to decimal.
    • Recursive program to convert hexadecimal number to decimal.

Methods – Convert hexadecimal number to integer in java

1.) Convert hexadecimal to decimal using Integer.parseInt

private static int toHexUsingParseInt(String number) {
 return Integer.parseInt(number, 16);
}

2.) Convert hexadecimal to decimal number – iterative

private static int iterative(String hexNumber) {
 int power = 0;
 int decimal = 0;
 hexNumber = hexNumber.toUpperCase();
 int length = hexNumber.length();
 String hexCode = "0123456789ABCDEF";
 for (int index = 0; index < length; index++) {
  char digit = hexNumber.charAt(length - index - 1);
  decimal = decimal + hexCode.indexOf(digit) * (int) Math.pow(16, power++);
 }
 return decimal;
}

3.) Convert hexadecimal to decimal number – recursive

private static int recursive(String hexNumber) {
 int decimal = 0;
 String hexCode = "0123456789ABCDEF";
 hexNumber = hexNumber.toUpperCase();
 int length = hexNumber.length();
 if (length > 0) {
  char ch = hexNumber.charAt(0);
  int digit = hexCode.indexOf(ch);
  String substring = hexNumber.substring(1);
  decimal = digit * (int) Math.pow(16, length - 1) + recursive(substring);
 }
 return decimal;
}

Complete program to convert hexadecimal to decimal number

package org.learn;

import java.util.Scanner;

public class HexaDecimalToDecimal {

 public static void main(String[] args) {
  try (Scanner scanner = new Scanner(System.in)) {

   System.out.printf("1. Enter hexadecimal string : ");
   String hex = scanner.next();

   int decimal = toHexUsingParseInt(hex);
   System.out.printf("2. Hex to decimal - parseInt(%s,16) = %d ", hex, decimal);

   decimal = iterative(hex);
   System.out.printf("\n3. Hex to decimal - iterative(%s) = %d ", hex, decimal);

   decimal = recursive(hex);
   System.out.printf("\n4. Hex to decimal - recursive(%s) = %d ", hex, decimal);
  }
 }

 private static int toHexUsingParseInt(String number) {
  return Integer.parseInt(number, 16);
 }

private static int iterative(String hexNumber) {
 int power = 0;
 int decimal = 0;
 hexNumber = hexNumber.toUpperCase();
 int length = hexNumber.length();
 String hexCode = "0123456789ABCDEF";
 for (int index = 0; index < length; index++) {
  char digit = hexNumber.charAt(length - index - 1);
  decimal = decimal + hexCode.indexOf(digit) * (int) Math.pow(16, power++);
 }
 return decimal;
}

 private static int recursive(String hexNumber) {
  int decimal = 0;
  String hexCode = "0123456789ABCDEF";
  hexNumber = hexNumber.toUpperCase();
  int length = hexNumber.length();
  if (length > 0) {
   char ch = hexNumber.charAt(0);
   int digit = hexCode.indexOf(ch);
   String substring = hexNumber.substring(1);
   decimal = digit * (int) Math.pow(16, length - 1) + recursive(substring);
  }
  return decimal;
 }
}

Program output – convert hexadecimal to decimal number 

1. Enter hexadecimal string : 1234
2. Hex to decimal - parseInt(1234,16) = 4660 
3. Hex to decimal - iterative(1234) = 4660 
4. Hex to decimal - recursive(1234) = 4660 

1. Enter hexadecimal string : 1a2B3
2. Hex to decimal - parseInt(1a2B3,16) = 107187 
3. Hex to decimal - iterative(1a2B3) = 107187 
4. Hex to decimal - recursive(1a2B3) = 107187 

1. Enter hexadecimal string : abcd
2. Hex to decimal - parseInt(abcd,16) = 43981 
3. Hex to decimal - iterative(abcd) = 43981 
4. Hex to decimal - recursive(abcd) = 43981 
Scroll to Top