Convert hex to decimal with Integer.parseInt()
Call Integer.parseInt() with the hex string and radix 16. The radix tells Java the string is base 16. Java reads the string as a signed 32-bit value, so the largest accepted hex is 7FFFFFFF (2,147,483,647).
Integer.parseInt("1A3F", 16) // 6719
Integer.parseInt("ff", 16) // 255 — case does not matter
Integer.parseInt("-FF", 16) // -255 — a leading minus is allowedThe 0x prefix trap
Integer.parseInt() rejects the 0x prefix. Passing "0xFF" throws a NumberFormatException because x is not a hex digit. Strip the prefix before parsing, or use Integer.decode(), which reads it.
Integer.parseInt("0xFF", 16) // NumberFormatException
Integer.decode("0xFF") // 255 — decode reads the prefixType a string in the picture to see which parse methods accept it.
NumberFormatException
Java throws NumberFormatException when the string contains characters outside 0–9 and A–F, when the value overflows the target type, or when the string is null or empty. Catch it when the input comes from a user, a file or an API (Application Programming Interface) response.
try {
int value = Integer.parseInt(input, 16);
} catch (NumberFormatException e) {
System.out.println("Not a valid hex string: " + input);
}Common causes:
- A
0xor#prefix that was not stripped. - A value above 7FFFFFFF for
parseIntor above 7FFFFFFFFFFFFFFF forLong.parseLong. - A null reference or an empty string.
- Whitespace around the digits, because
parseIntdoes not trim.
Unsigned and large values
Integer.parseInt() treats the hex as a signed 32-bit integer, so it rejects anything above 7FFFFFFF. Java offers three ways to handle larger values.
Integer.parseUnsignedInt (Java 8+)
parseUnsignedInt reads up to FFFFFFFF and stores all 32 bits in an int. The stored bit pattern may print as negative because Java's int is signed.
Integer.parseUnsignedInt("FFFFFFFF", 16) // -1 (all 32 bits set)
Integer.toUnsignedString(-1, 16) // "ffffffff"Long.parseLong
Long.parseLong reads hex values up to 7FFFFFFFFFFFFFFF (9,223,372,036,854,775,807). It returns a long, which is a signed 64-bit integer.
Long.parseLong("FFFFFFFF", 16) // 4294967295
Long.parseLong("1A2B3C4D5E", 16) // 112394521950BigInteger
BigInteger accepts hex strings of any length. Pass the string and radix 16 to its constructor.
import java.math.BigInteger;
BigInteger value = new BigInteger("FFFFFFFFFFFFFFFF", 16);
System.out.println(value); // 18446744073709551615BigInteger returns an object, not a primitive, so arithmetic uses methods such as add() and multiply() instead of + and *.
Signed hex to decimal
Cast a wider parse result down to the target width, and Java reads the bits as a signed value.
(byte) Integer.parseInt("FF", 16) // -1 (8-bit signed)
(short) Integer.parseInt("FF9C", 16) // -100 (16-bit signed)
(int) Long.parseLong("FFFFFFFF", 16) // -1 (32-bit signed)The cast keeps only the lower bits and interprets them as two's complement. Long.parseLong("FFFFFFFF", 16) returns 4,294,967,295 as a long, but casting it to int gives −1 because all 32 bits are set.
For a full breakdown of sign bits and two's complement at every width, see the signed hex to decimal converter.
Integer.decode()
Integer.decode() detects the base from the prefix: 0x and 0X for hex, 0 for octal, # for hex. It returns an Integer object and works in contexts where the input format is not known in advance.
Integer.decode("0xFF") // 255 (hex)
Integer.decode("#FF") // 255 (hex)
Integer.decode("0377") // 255 (octal)
Integer.decode("255") // 255 (decimal, no prefix)Long.decode() works the same way for 64-bit values. Neither method accepts whitespace or a trailing type suffix. Watch out for a leading zero: Integer.decode("0377") is octal, not decimal.
Decimal to hex in Java
Use Integer.toHexString() to convert a decimal integer back to a hex string. The result is lowercase with no prefix.
Integer.toHexString(255) // "ff"
Integer.toHexString(255).toUpperCase() // "FF"
String.format("%04X", 255) // "00FF"
String.format("0x%X", 6719) // "0x1A3F"| Expression | Output | Use case |
|---|---|---|
Integer.toHexString(n) | ff | Quick lowercase hex |
.toUpperCase() | FF | Display and logging |
String.format("%04X", n) | 00FF | Fixed-width uppercase |
Long.toHexString(n) | ffffffff | Values above 2³¹ − 1 |
For the full reverse conversion method and worked examples, see the decimal to hex converter.
Method comparison
| Method | Max hex | Signed | Accepts 0x | On overflow |
|---|---|---|---|---|
Integer.parseInt(s, 16) | 7FFFFFFF | Yes (signed int) | No | NumberFormatException |
Integer.parseUnsignedInt(s, 16) | FFFFFFFF | No (unsigned bits in int) | No | NumberFormatException |
Long.parseLong(s, 16) | 7FFFFFFFFFFFFFFF | Yes (signed long) | No | NumberFormatException |
Long.parseUnsignedLong(s, 16) | FFFFFFFFFFFFFFFF | No | No | NumberFormatException |
new BigInteger(s, 16) | No limit | Yes (positive by default) | No | Never |
Integer.decode(s) | 7FFFFFFF | Yes | Yes | NumberFormatException |
Start with Integer.parseInt(s, 16) for values up to 7FFFFFFF. Use Long.parseLong for 8-digit hex values that exceed 2³¹ − 1, and BigInteger for anything longer than 16 digits.
Try every method on one string
Type one string and see how every method in the table reacts.
Frequently asked questions
How do you convert hex to decimal in Java?
Integer.parseInt("FF", 16) to convert the hex string "FF" to the decimal integer 255. The second argument, 16, is the radix that tells Java the input is base 16.Why does Integer.parseInt throw NumberFormatException for 0xFF?
parseInt does not accept the 0x prefix, because the x is not a valid hex digit. Strip the prefix with s.replaceFirst("^0[xX]", "") before parsing, or use Integer.decode("0xFF"), which reads the prefix and returns 255.What is the largest hex value Integer.parseInt accepts?
int. Use Long.parseLong(s, 16) for values up to 7FFFFFFFFFFFFFFF, or new BigInteger(s, 16) for unlimited length.How do you convert signed hex to decimal in Java?
(int) Long.parseLong("FFFFFFFF", 16) returns −1. The cast keeps the lower 32 bits and reads them as two's complement. For 8-bit or 16-bit signed values, cast to byte or short.What is the difference between parseInt and decode?
parseInt takes a radix argument and rejects any prefix. decode reads the prefix (0x, 0, #) and picks the radix automatically. Use parseInt when the radix is known, and decode when the input may carry different prefixes.Does Java support unsigned hex conversion?
Integer.parseUnsignedInt("FFFFFFFF", 16) stores all 32 bits in an int. The value prints as −1 through println because Java's int is signed, but Integer.toUnsignedString(-1, 16) prints "ffffffff".Related tools
- Hex to decimal converter — convert hex to decimal with digit-by-digit steps
- Signed hex to decimal — interpret hex as a signed two's complement integer
- Hex to decimal in Python — int(), bytes and struct methods
- Hex to decimal in JavaScript — parseInt, Number and BigInt methods