How to Convert Decimal to Hexadecimal in Java
In today's tutorial we'll learn about how we can convert decimal numbers to hexadecimal representation in Java. As usual, we'll look at various ways of accomplishing this task.
Writing a Custom Dec to Hex Function from Scratch
Let's first write our own function to convert decimal numbers to hexadecimal. Certainly, that's not something that you'd use in production code, but it's worth seeing and understanding the algorithm.
So, let's get started. How do we convert decimal values to hexadecimal?
Hexadecimal representation of an integer value can be done by performing the following steps:
- Carry out modulo division on the number by the base (which is 16 in this case). The result (remainder) will specify the hexadecimal character.
- Divide the number by 16
- If the result of the division is not 0, Jump to #1
And there’s one more step left: You’ll need to reverse the characters.
The logic above could be converted into actual code as follows:
public String convertDecToHex(long number) {
String hexChars = "0123456789ABCDEF";
StringBuffer sb = new StringBuffer();
int i = 0;
do {
sb.append(hexChars.charAt((int) (number % 16)));
number /= 16;
} while (number > 0);
return sb.reverse().toString().toLowerCase();
}
And it works as expected:

Using Integer.toHexString
There's of course out-of-the box solution in the Java libraries.
The static method toHexString of the Integer (or Long) class just does what it says: converts integers to hexadecimal representation.
public static void main(String[] args) {
System.out.println(Integer.toHexString(7864));
}

Using String.format
The format method of the String class returns the formatted string according to the format string and the respective parameters.
The %x format specifier will return the given number in hexadecimal format:
public static void main(String[] args) {
System.out.println(String.format("%x", 7864));
}

Benchmarking the Solutions
If we look into the internals of Integer.toHexString and String.format we'll see that the %x format specifier of the format method basically re-uses the Long.toHexString method. That's obvious, why would it use another implementation?
} else if (c == Conversion.HEXADECIMAL_INTEGER) {
checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE,
Flags.PLUS);
String s = Long.toHexString(value);
int len = (f.contains(Flags.ALTERNATE)
? s.length() + 2
: s.length());
// ...
}
The implementation of the Long.toHexString method uses bitwise operations to be as fast as possible.
To reveal the performance difference between the toHexString and our from scratch implementations, I created a micro-benchmark that converts 10 000 000 numbers to hexadecimal using both implementations.
Considering that we did not use any trick in our implementation to speed up the conversion, the test result is a bit surprising: The toHexString implementation finished in 12.1 seconds while the our implementation finished in 13.7 seconds.
So even if we did not manage to beat the out implementation shipped with Java, it's very interesting that we got so close to it.
It'd be worth looking into the bytecode the compiler generates — most likely it has found several ways to optimize it.
Conclusion
In this post we learned about how we can convert decimal numbers to hexadecimal in Java and implemented our function from scratch.
We've also seen two methods Java provide out-of-the-box that can be used.