Convert decimal to hexadecimal – java api, iterative & recursive

  • Given a decimal number (base 10), convert decimal to hexadecimal number (base 16) using java.
  • We will discuss various methods to convert decimal number to hexadecimal number.
    • Convert decimal to hexadecimal using Integer class
      • Integer.toHexString
      • Integer.toString
    • Iterative program to convert decimal number to hexadecimal number.
    • Recursive method to convert decimal number to hexadecimal number.

Methods – convert decimal to hexadecimal in java

1.) Convert decimal to hexadecimal using Integer.toHexString

private static String toHexString(int number) {
	return Integer.toHexString(number);
}

2.) Convert decimal to hexadecimal using Integer.toString

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

3.) Convert decimal to hexadecimal using iterative method

private static String iterative(int number) {
	StringBuilder builder = new StringBuilder();
	String hexCode = "0123456789ABCDEF";
	
	while (number > 0) {
		int hexDigit = number % 16;			
		builder.append(hexCode.charAt(hexDigit));
		number /= 16;
	}
	return builder.reverse().toString();
}

4.) Convert decimal to hexadecimal using recursive method

private static String recursive(int number) {
	 StringBuilder builder = new StringBuilder();
    if (number>0) {
    	String hexNumber = recursive(number/16);
        String hexCode = "0123456789ABCDEF";
		int hexDigit = number % 16;		
		builder.append(hexNumber + hexCode.charAt(hexDigit));
    }
    return builder.toString();
}

Complete program to convert decimal to hexadecimal

package org.learn;

import java.util.Scanner;

public class DecimalToHexDecimal {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.printf("1. Enter decimal number : ");
			int number = scanner.nextInt();
			String hexadecimal = toHexString(number);

			System.out.printf("2. Decimal to hexadecimal - toBinaryString(%d) = %s ", number, hexadecimal);

			hexadecimal = toString(number);
			System.out.printf("\n3. Decimal to hexadecimal - toString(%d) = %s ", number, hexadecimal);

			hexadecimal = iterative(number);
			System.out.printf("\n4. Decimal to hexadecimal - iterative(%d) = %s ", number, hexadecimal);

			hexadecimal = recursive(number);
			System.out.printf("\n5. Decimal to hexadecimal - recursive(%d) = %s ", number, hexadecimal);
		}
	}

	private static String toHexString(int number) {
		return Integer.toHexString(number);
	}

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

	private static String iterative(int number) {
		StringBuilder builder = new StringBuilder();
		String hexCode = "0123456789ABCDEF";

		while (number > 0) {
			int hexDigit = number % 16;
			builder.append(hexCode.charAt(hexDigit));
			number /= 16;
		}
		return builder.reverse().toString();
	}

	private static String recursive(int number) {
		StringBuilder builder = new StringBuilder();
		if (number > 0) {
			String hexNumber = recursive(number / 16);
			String hexCode = "0123456789ABCDEF";
			int hexDigit = number % 16;
			builder.append(hexNumber + hexCode.charAt(hexDigit));
		}
		return builder.toString();
	}
}

Program output – convert decimal to hexadecimal

1. Enter decimal number : 25
2. Decimal to hexadecimal - toBinaryString(25) = 19 
3. Decimal to hexadecimal - toString(25) = 19 
4. Decimal to hexadecimal - iterative(25) = 19 
5. Decimal to hexadecimal - recursive(25) = 19 

1. Enter decimal number : 31
2. Decimal to hexadecimal - toBinaryString(31) = 1f 
3. Decimal to hexadecimal - toString(31) = 1f 
4. Decimal to hexadecimal - iterative(31) = 1F 
5. Decimal to hexadecimal - recursive(31) = 1F 

1. Enter decimal number : 255
2. Decimal to hexadecimal - toBinaryString(255) = ff 
3. Decimal to hexadecimal - toString(255) = ff 
4. Decimal to hexadecimal - iterative(255) = FF 
5. Decimal to hexadecimal - recursive(255) = FF 
Scroll to Top