0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert Decimal to Hexadecimal

In Java, convert a decimal number to hexadecimal using Integer.toHexString(decimalNumber), which returns the hexadecimal string representation.
📋

Examples

Input255
Outputff
Input16
Output10
Input0
Output0
🧠

How to Think About It

To convert decimal to hexadecimal, think of dividing the decimal number by 16 repeatedly and collecting remainders. Java simplifies this by providing a method that does this conversion internally and returns the hexadecimal string.
📐

Algorithm

1
Get the decimal number as input.
2
Use the built-in method to convert the decimal number to a hexadecimal string.
3
Return or print the hexadecimal string.
💻

Code

java
public class DecimalToHex {
    public static void main(String[] args) {
        int decimalNumber = 255;
        String hexString = Integer.toHexString(decimalNumber);
        System.out.println("Hexadecimal: " + hexString);
    }
}
Output
Hexadecimal: ff
🔍

Dry Run

Let's trace converting decimal 255 to hexadecimal using Integer.toHexString.

1

Input decimal number

decimalNumber = 255

2

Convert to hexadecimal string

hexString = Integer.toHexString(255) -> "ff"

3

Print result

Output: Hexadecimal: ff

Decimal NumberHexadecimal String
255ff
💡

Why This Works

Step 1: Using built-in method

The Integer.toHexString() method converts the decimal integer to its hexadecimal string form internally.

Step 2: Returns lowercase hex

The method returns the hexadecimal string in lowercase letters by default.

Step 3: Simple and efficient

This approach avoids manual division and remainder calculations, making the code clean and fast.

🔄

Alternative Approaches

Manual division and remainder
java
public class DecimalToHexManual {
    public static void main(String[] args) {
        int decimal = 255;
        String hex = "";
        char[] hexChars = "0123456789abcdef".toCharArray();
        while (decimal > 0) {
            int remainder = decimal % 16;
            hex = hexChars[remainder] + hex;
            decimal /= 16;
        }
        System.out.println("Hexadecimal: " + (hex.isEmpty() ? "0" : hex));
    }
}
This method shows the manual process but is longer and more error-prone than using built-in methods.
Using String.format
java
public class DecimalToHexFormat {
    public static void main(String[] args) {
        int decimal = 255;
        String hex = String.format("%x", decimal);
        System.out.println("Hexadecimal: " + hex);
    }
}
This uses formatting to convert decimal to hex, which is concise and readable.

Complexity: O(log n) time, O(log n) space

Time Complexity

Conversion depends on the number of digits in hexadecimal, which is proportional to log base 16 of the decimal number.

Space Complexity

The output string size grows with the number of hex digits, also proportional to log base 16 of the input.

Which Approach is Fastest?

Using built-in methods like Integer.toHexString() or String.format is faster and less error-prone than manual conversion.

ApproachTimeSpaceBest For
Integer.toHexString()O(log n)O(log n)Simple and fast conversion
Manual division and remainderO(log n)O(log n)Learning and understanding conversion process
String.formatO(log n)O(log n)Concise formatting-based conversion
💡
Use Integer.toHexString() for quick and easy decimal to hexadecimal conversion in Java.
⚠️
Beginners often forget that toHexString returns a lowercase string and may expect uppercase letters.