To convert a decimal number to hexadecimal, we divide it by 16 again and again, write down each remainder as a hex digit (10 is A, 11 is B, up to 15 is F), and read the remainders from the last one to the first. Hexadecimal is the base-16 number system, so it uses the digits 0-9 and the letters A-F. For example, the decimal to hexadecimal conversion of 269 gives 10D in three divisions.
| Step | Division | Quotient | Remainder | Hex digit |
|---|---|---|---|---|
| 1 | 269 / 16 | 16 | 13 | D |
| 2 | 16 / 16 | 1 | 0 | 0 |
| 3 | 1 / 16 | 0 | 1 | 1 |
We stop when the quotient is 0 and read the hex digits from step 3 up to step 1, which gives 10D. To check the result, we multiply each digit by its power of 16 and add the products, which gives 1 x 256 + 0 x 16 + 13 x 1 = 269.
The same method gives the decimal to hexadecimal conversion examples in the next table. We meet these values in color codes (255 is FF), memory addresses, byte dumps and file formats.
| Decimal | Hexadecimal | Decimal | Hexadecimal |
|---|---|---|---|
| 10 | A | 256 | 100 |
| 15 | F | 269 | 10D |
| 16 | 10 | 1000 | 3E8 |
| 26 | 1A | 4095 | FFF |
| 100 | 64 | 4096 | 1000 |
| 255 | FF | 65535 | FFFF |
The following example does the decimal to hexadecimal conversion in Java with the built-in methods of the JDK (Java 25), and converts the result back to decimal.
String hex = Integer.toHexString(269); // "10d"
String upper = Integer.toHexString(269).toUpperCase(); // "10D"
String padded = String.format("%04X", 269); // "010D"
String big = Long.toHexString(4_294_967_296L); // "100000000"
String negative = Integer.toHexString(-1); // "ffffffff" (two's complement)
int decimal = Integer.parseInt("10D", 16); // 269
Notice that the hexadecimal result is always a String, because a hexadecimal number is only a text representation of a value. A negative int comes out as its 32-bit two’s complement form, not with a minus sign.
In the rest of this tutorial, we look at the division method in more detail, the other Java options such as BigInteger and String.format(), a custom conversion method, and the reverse conversion from hexadecimal to decimal.
1. Difference between Decimal and Hexadecimal Numbers
Decimal and hexadecimal are two ways to write the same value, and they differ only in the base. Knowing the base explains every step of the conversion.
1.1. Difference
A decimal number uses 10 symbols (base 10) ranging from [0-9] to represent a number, while a hexadecimal number uses 16 symbols (base 16) ranging from [0-9, A-F].
The radix (or base) of a decimal number is 10, so all digits of a decimal number are represented as powers of 10. The radix of a hexadecimal number is 16, so all digits of a hexadecimal number are represented as powers of 16.

1.2. Mathematical Conversion
As the hexadecimal numbers are represented as powers of 16, we can divide a given decimal number by 16 and keep the remainder to get the corresponding hexadecimal number. Let’s walk through, step by step, the conversion of 269 (a decimal number) to hexadecimal.
- Take 269 as the dividend and 16 as the divisor.
- Divide the dividend by the divisor and store the remainder.
- Divide the quotient obtained from the above step by 16 and store the remainder.
- Repeat the third step until the quotient becomes zero.
- The final hexadecimal number is the reverse of the stored remainders.

2. Representing Numbers of Different Bases in Java
In Java, the number types (int, long, float, double) hold a value, not a format, and printing them always shows the value in decimal form. It’s never possible that an int/float can hold any number in binary, octal, or hexadecimal format.
We can assign binary, octal, or hexadecimal values to an int/long using predefined prefixes.
int hexNum = 0X10D; // '0X' or '0x' prefix to represent hexadecimal number
int octNumber = 01560; // '0' prefix to represent octal number
Still, the compiler stores both as plain int values, and printing that variable will print the value in the decimal form only.
System.out.println(hexNum); // 269
System.out.println(octNumber); // 880
So we can never get a hexadecimal, octal, or binary representation of any number in an int or other number data types, and we represent hex/octal/binary numbers in String format only.
3. Converting Decimal to Hexadecimal in Java
Let us see a few methods to convert a given decimal to hexadecimal in Java.
3.1. Using Number Classes
All Java number classes provide built-in methods for performing the conversion from decimal to equivalent hex number. For the Integer, Long and BigInteger classes, we have these methods.
- Integer.toString(num, radix);
- Integer.toHexString(num);
- Long.toString(num, radix);
- Long.toHexString(num);
- BigInteger.toString(radix);
For the Float and Double classes, we have these methods.
- Float.toHexString(num);
- Double.toHexString(num);
In toString() methods, we pass the radix (16 for hexadecimal) along with the number to convert.
int number = 269;
String hex = Integer.toString(number, 16); // '10d'
The toHexString() method converts the given number into hexadecimal form with no radix argument. For float and double, the result is the hexadecimal floating-point literal format.
double number = 333.44d;
String hex = Double.toHexString(number); // '0x1.4d70a3d70a3d7p8'
The BigInteger class also provides a toString(radix) method for the same purpose.
BigInteger number = new BigInteger("269");
String hex = number.toString(16); // '10d'
3.2. Using String.format()
The String class has an overloaded format() method that accepts a format specifier. The format to represent hexadecimal numbers is %x, and %X prints the letters in uppercase. This method can be used to convert a decimal integer number into a hexadecimal string.
int number = 269;
String hex = String.format("%x", number); // '10d'
3.3. Using a Custom Method
We can also create a custom method and code for the algorithm discussed in section 1 of this article. The method accepts non-negative numbers and throws IllegalArgumentException for a negative one.
- Perform the long division of the decimal number.
- Take the remainder of the division.
- Get the hexadecimal token according to the remainder.
- Prepend the token into the result.
- Repeat these steps until the result of the division is 0.
public static String convertToHex(int number) {
if (number < 0) {
throw new IllegalArgumentException("Negative number: " + number);
}
if (number == 0) {
return "0";
}
int remainder;
StringBuilder result = new StringBuilder();
String tokens = "0123456789ABCDEF";
while (number > 0) {
remainder = number % 16;
result.insert(0, tokens.charAt(remainder));
number = number / 16;
}
return result.toString();
}
We can use this method for the decimal-to-hex conversion in Java.
String hex = convertToHex(269); // "10D"
String zero = convertToHex(0); // "0"
String bad = convertToHex(-5); // IllegalArgumentException: Negative number: -5
4. Converting a Hexadecimal Number to Decimal
The reverse conversion multiplies each hex digit by its power of 16 and adds the results, so 10D gives 1 x 256 + 0 x 16 + 13 = 269.
4.1. Using Number Classes
Converting from hexadecimal strings to Java number types takes one method call for the Integer and Long types. There is direct API support for this conversion.
- Integer.parseInt(hex, radix);
- Integer.valueOf(hex, radix);
- Integer.decode(hex);
- Long.parseLong(hex, radix);
- Long.valueOf(hex, radix);
- Long.decode(hex);
- new BigInteger(hex, radix);
The following example converts a hexadecimal to the Integer type using the defined methods. The same can be done for hexadecimal to Long conversion. The decode() method expects the “0x”, “0X” or “#” prefix in front of the hex digits.
String hex = "10D";
int fromParseInt = Integer.parseInt(hex, 16); // 269
int fromValueOf = Integer.valueOf(hex, 16); // 269
int fromDecode = Integer.decode("0x" + hex); // 269
All three methods throw NumberFormatException when the text contains a character that is not a hex digit, e.g. Integer.parseInt(“10G”, 16). When the input comes from a user, we call strip() on it first and catch NumberFormatException.
The BigInteger class also provides a constructor new BigInteger(hex, radix) for the same purpose.
BigInteger decimal = new BigInteger("10d",16);
System.out.println(decimal); // 269
Converting a hexadecimal to floating-point numbers (float and double) takes two steps.
- Parse the hexadecimal string to an integer.
- Use Float.intBitsToFloat() or Double.longBitsToDouble() to interpret the integer as a float or double value.
String myString = "BF800000";
Long i = Long.parseLong(myString, 16);
Float f = Float.intBitsToFloat(i.intValue());
//converted float value
System.out.println(f);
//Convert the float back to hex and verify
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
The program prints the float value and the hex string again.
-1.0
bf800000
4.2. Using a Custom Method
To convert a hexadecimal string into a decimal integer using the raw method, we use the following algorithm.
- Find the decimal number of the corresponding hexadecimal token.
- Multiply the result so far by 16, and add the decimal number of the token.
- Repeat these steps till the last token of the hexadecimal string.
The method rejects empty input and any character outside 0-9 and A-F with an IllegalArgumentException, so a typo never turns into a wrong number.
public static int convertHexToDecimal(String hex) {
String tokens = "0123456789ABCDEF";
hex = hex.strip().toUpperCase();
if (hex.isEmpty()) {
throw new IllegalArgumentException("Empty hex string");
}
int result = 0;
for (int i = 0; i < hex.length(); i++) {
int n = tokens.indexOf(hex.charAt(i));
if (n < 0) {
throw new IllegalArgumentException("Not a hex digit: " + hex.charAt(i));
}
result = result * 16 + n;
}
return result;
}
Let us test the method.
int decimal = convertHexToDecimal("10D"); // 269
int bad = convertHexToDecimal("10G"); // IllegalArgumentException: Not a hex digit: G
5. Conclusion
In this Java data types tutorial, we learned about the difference between decimal and hexadecimal numbers, and the conversion of decimal to hexadecimal numbers using the APIs and custom methods.
By hand, we divide by 16 and read the remainders in reverse. In Java, Integer.toHexString() or String.format(“%X”) does the decimal to hexadecimal conversion in one line, and Integer.parseInt(hex, 16) converts the hex string back.
6. References
Happy Learning !!