Hex to Decimal

Hex to decimal in Java

Type a hex value and pick a method. The output shows the printed value, or the exception Java throws.

Integer.parseInt

Output:

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 allowed

The 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 prefix

Type 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 0x or # prefix that was not stripped.
  • A value above 7FFFFFFF for parseInt or above 7FFFFFFFFFFFFFFF for Long.parseLong.
  • A null reference or an empty string.
  • Whitespace around the digits, because parseInt does 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)   // 112394521950

BigInteger

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);   // 18446744073709551615

BigInteger 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"
ExpressionOutputUse case
Integer.toHexString(n)ffQuick lowercase hex
.toUpperCase()FFDisplay and logging
String.format("%04X", n)00FFFixed-width uppercase
Long.toHexString(n)ffffffffValues above 2³¹ − 1

For the full reverse conversion method and worked examples, see the decimal to hex converter.

Method comparison

MethodMax hexSignedAccepts 0xOn overflow
Integer.parseInt(s, 16)7FFFFFFFYes (signed int)NoNumberFormatException
Integer.parseUnsignedInt(s, 16)FFFFFFFFNo (unsigned bits in int)NoNumberFormatException
Long.parseLong(s, 16)7FFFFFFFFFFFFFFFYes (signed long)NoNumberFormatException
Long.parseUnsignedLong(s, 16)FFFFFFFFFFFFFFFFNoNoNumberFormatException
new BigInteger(s, 16)No limitYes (positive by default)NoNever
Integer.decode(s)7FFFFFFFYesYesNumberFormatException

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?
Call 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?
7FFFFFFF, which is 2,147,483,647 in decimal. Any hex above this overflows a signed 32-bit 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?
Parse with a wider type and cast down: (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?
Yes, since Java 8. 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".