Convert Decimal to Hexadecimal in Java

Topic

In this Java program, you'll learn how to convert a decimal number (base 10) into its hexadecimal equivalent (base 16). Hexadecimal numbers use digits 0–9 and letters A–F to represent values. For example, the decimal number 15 becomes F in hexadecimal, and 255 becomes FF.

This conversion is commonly used in areas like memory addressing, color codes (like #FFFFFF), and low-level programming. Java provides built-in methods to handle this conversion, but we’ll also explore how to do it manually.

Examples

Example 1:
Decimal: 10
Hexadecimal: A

Example 2:
Decimal: 255
Hexadecimal: FF

Example 3 (Edge Case):
Decimal: 0
Hexadecimal: 0
Explanation: 0 in decimal is 0 in hexadecimal as well.

Interviewer Expectations

The interviewer wants to test your understanding of:

  • Number system conversions
  • Modulus and division operators
  • Looping constructs (like while)
  • Character and string manipulation
  • Handling edge cases like 0

Approach

The hexadecimal number system has 16 digits: 0–9 and A–F. To convert a decimal to hexadecimal:

  1. Repeatedly divide the number by 16.
  2. Store the remainder each time.
  3. Map remainders 10–15 to A–F.
  4. Reverse the remainders to get the final hexadecimal value.

Dry Run for 26:

  • 26 ÷ 16 = 1 remainder 10 → ‘A’
  • 1 ÷ 16 = 0 remainder 1 → ‘1’
  • Reversed → 1A

Java Program

public class DecimalToHexadecimal {
    public static void main(String[] args) {
        int decimal = 26;
        String hex = decimalToHex(decimal);
        System.out.println("Hexadecimal of " + decimal + " is: " + hex);
    }

    public static String decimalToHex(int number) {
        if (number == 0) return "0";

        StringBuilder hex = new StringBuilder();
        char[] hexChars = "0123456789ABCDEF".toCharArray();

        while (number > 0) {
            int remainder = number % 16;
            hex.insert(0, hexChars[remainder]);
            number = number / 16;
        }

        return hex.toString();
    }
}
Hexadecimal of 26 is: 1A

Possible Followup Questions with Answers

Q1: Can we use a built-in method in Java to convert decimal to hexadecimal?

Yes. You can use Integer.toHexString(int) to directly convert:

System.out.println(Integer.toHexString(255)); // Outputs: ff

Q2: How can you handle negative numbers?

Hexadecimal is typically used for unsigned values. To handle negatives properly (e.g., for bit manipulation), you can use:

System.out.println(Integer.toHexString(-1)); // Outputs: ffffffff (32-bit 2's complement)

Q3: What if I want the output in uppercase?

String hex = Integer.toHexString(255).toUpperCase();

Q4: Can I convert hexadecimal back to decimal?

Yes, use Integer.parseInt(hexString, 16):

int decimal = Integer.parseInt("FF", 16); // Outputs: 255

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...