Site icon

Convert decimal number to hexdecimal – (java api, iterative & recursive)

Java JDK methods to convert decimal to hexadecimal:

Method Name Description
String toHexString(int i) Returns a string representation of the integer argument as an unsigned integer in base 16.
String toString(int i, int radix) Returns a string representation of the first argument in the radix specified by the second argument.

Methods to convert decimal integer to hexadecimal :

1.) Convert decimal to hexadecimal using Integer.toHexString

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

2.) Convert decimal to hexadecimal using Integer.toString

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

3.) Convert decimal to hexadecimal using iterative method

private static String iterative(int number) {

	StringBuilder builder = new StringBuilder();
	while (number > 0) {
		int remainder = number % 16;
		char hexCode = hexCodes[remainder];
		builder.append(hexCode);
		number = 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);
		int digit = number % 16;
		char hexDigit = hexCodes[digit];
		builder.append(hexNumber + hexDigit);
	}
	return builder.toString();
}

Complete program to convert decimal number to hexadecimal

package org.learn;

import java.util.Scanner;

public class DecimalToHex {
    private static char[] hexCodes = {'0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F'};

    private static String iterative(int number) {

        StringBuilder builder = new StringBuilder();
        while (number > 0) {
            int remainder = number % 16;
            char hexCode = hexCodes[remainder];
            builder.append(hexCode);
            number = number / 16;
        }
        return builder.reverse().toString();
    }

    private static String recursive(int number) {
        StringBuilder builder = new StringBuilder();

        if (number > 0) {
            String hexNumber = recursive(number / 16);
            int digit = number % 16;
            char hexDigit = hexCodes[digit];
            builder.append(hexNumber + hexDigit);
        }
        return builder.toString();
    }

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

    private static final String toHexString(int number) {
        return Integer.toHexString(number).toUpperCase();
    }
    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 hex = toHexString(number);

            System.out.printf("2. Hex of (%d) using toHexString method = %s\n", number, hex);

            hex = toString(number);
            System.out.printf("3. Hex of (%d) using toString method = %s\n", number, hex);

            hex = iterative(number);
            System.out.printf("4. Hex of (%d) using iterative method = %s\n", number, hex);

            hex = recursive(number);
            System.out.printf("5. Hex of (%d) using recursive method = %s\n", number, hex);
        }
    }
}

Program output –  convert decimal number to hexadecimal

1. Enter decimal number : 100
2. Hex of (100) using toHexString method = 64
3. Hex of (100) using toString method = 64
4. Hex of (100) using iterative method = 64
5. Hex of (100) using recursive method = 64

1. Enter decimal number : 255
2. Hex of (255) using toHexString method = FF
3. Hex of (255) using toString method = FF
4. Hex of (255) using iterative method = FF
5. Hex of (255) using recursive method = FF
Exit mobile version