Hex to Decimal

Hex to decimal in JavaScript

Type a hex value and pick a method. The output shows exactly what the browser prints, rounding included.

parseInt

Output:

Convert hex to decimal with parseInt()

Call parseInt() with the hex string and radix 16. The radix tells JavaScript the string is base 16 (hexadecimal). Always pass 16 explicitly: without it, parseInt reads plain digits as decimal.

parseInt("1A3F", 16)      // 6719
parseInt("ff", 16)         // 255 — case does not matter
parseInt("0xFF", 16)       // 255 — the 0x prefix is accepted
parseInt("0x1A3F", 16)    // 6719

Partial parsing and NaN

parseInt reads characters from left to right and stops at the first character that is not a valid hex digit, without throwing an error.

parseInt("FFzz", 16)   // 255 — stops at "z"
parseInt("zz", 16)     // NaN — no valid digits at all
parseInt("", 16)       // NaN — empty string

This silent truncation can hide bugs. Validate the input with a regular expression before parsing when the value comes from a user or an external source:

/^(0x)?[0-9a-f]+$/i.test(input)

Number() with the 0x prefix

Number() converts a hex string to a number when the string starts with 0x. It rejects any character that is not part of the hex literal, so it returns NaN (Not a Number) for partial input instead of silently truncating.

Number("0xFF")      // 255
Number("0x1A3F")   // 6719
Number("0xGG")     // NaN — invalid character
Number("FF")       // NaN — no 0x prefix

Use Number() when the input already has the 0x prefix and you want strict validation. Use parseInt() when the input may or may not have the prefix.

Large values and BigInt

JavaScript's Number type stores integers exactly up to 2⁵³ − 1 (9,007,199,254,740,991), which is 0x1FFFFFFFFFFFFF in hex. Past that limit, parseInt and Number round the result. A 64-bit identifier, a cryptographic hash or a memory address longer than 14 hex digits needs BigInt.

parseInt("20000000000001", 16)              // 9007199254740992 — rounded
BigInt("0x20000000000001")                  // 9007199254740993n — exact
BigInt("0xFFFFFFFFFFFFFFFF").toString()     // "18446744073709551615"

BigInt needs the 0x prefix when parsing hex. The result is a BigInt value, not a Number, so it cannot be mixed with regular numbers in arithmetic without explicit conversion.

Signed 64-bit with BigInt

BigInt.asIntN() reads a BigInt as a signed two's complement value at a given bit width.

BigInt.asIntN(64, BigInt("0xFFFFFFFFFFFFFFFF"))   // -1n
BigInt.asIntN(32, BigInt("0xFFFFFFFF"))            // -1n
BigInt.asIntN(16, BigInt("0xFF9C"))                // -100n

Signed hex to decimal

The bitwise OR operator | 0 forces a value into a signed 32-bit integer, which gives the two's complement reading.

parseInt("FFFFFFFF", 16) | 0    // -1
parseInt("80000000", 16) | 0    // -2147483648
parseInt("FF9C0000", 16) | 0    // -6553600

For other bit widths, write the value into a typed array and read it back:

new Int8Array([0xFF])[0]             // -1 (8-bit signed)
new Int16Array([0xFF9C])[0]          // -100 (16-bit signed)
new Int32Array([0xFFFFFFFF])[0]      // -1 (32-bit signed)

Typed arrays interpret the bits at the array's fixed width, so Int8Array gives an 8-bit signed result and Int16Array gives a 16-bit signed result.

For a full breakdown of sign bits and two's complement at every width, see the signed hex to decimal converter.

Handling input formats

Hex values in the wild come with different prefixes and formatting. Strip them before parsing.

function hexToDec(s) {
  s = s.trim().replace(/^(0x|#)/i, "");
  if (!/^[0-9a-f]+$/i.test(s)) return NaN;
  return parseInt(s, 16);
}

hexToDec("0xFF")      // 255
hexToDec("#FF")        // 255 — CSS colour prefix
hexToDec("  ff  ")     // 255 — whitespace
hexToDec("GG")         // NaN — invalid

CSS colour codes use # instead of 0x. The function above handles both. For colour-specific conversion with RGB channels, see the hex to RGB converter.

Decimal to hex in JavaScript

Use toString(16) to convert a decimal number back to a hex string. The result is lowercase with no prefix.

(255).toString(16)                        // "ff"
(255).toString(16).toUpperCase()          // "FF"
(255).toString(16).padStart(4, "0")       // "00ff"
"0x" + (6719).toString(16).toUpperCase()  // "0x1A3F"

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

Method comparison

MethodNeeds 0xPartial parsePrecision limitSigned
parseInt(s, 16)NoSilently truncates2⁵³ − 1No
Number("0x" + s)YesReturns NaN2⁵³ − 1No
BigInt("0x" + s)YesThrows errorNoneWith asIntN
parseInt(s, 16) | 0NoTruncates32-bit onlyYes (32-bit)
Int8Array / Int16ArrayNoN/A (numeric input)Fixed widthYes

Start with parseInt(s, 16) for most conversions. Reach for BigInt when the hex value exceeds 1FFFFFFFFFFFFF, and use | 0 or a typed array when you need a signed result.

Type one string and compare all five methods side by side. Try FFzz for partial parsing and FFFFFFFFFFFFFFFF for precision.

Frequently asked questions

How do you convert hex to decimal in JavaScript?
Call parseInt("FF", 16) to convert the hex string "FF" to the decimal number 255. The second argument, 16, is the radix that tells parseInt the input is base 16.
What is the radix parameter in parseInt?
The radix is the number base of the input string. Pass 16 for hex, 10 for decimal, 8 for octal and 2 for binary. Always pass the radix explicitly: without it, parseInt reads plain digits as decimal, and only a leading 0x switches it to hex.
Why does parseInt return NaN?
parseInt returns NaN when no valid digits appear at the start of the string. parseInt("zz", 16) is NaN because "z" is not a hex digit. An empty string also returns NaN.
How do you convert signed hex to decimal in JavaScript?
Use parseInt("FFFFFFFF", 16) | 0 for 32-bit signed values, which returns −1. The bitwise OR forces the value into a signed 32-bit integer. For other widths, use new Int8Array([value])[0] or BigInt.asIntN(bits, bigintValue).
How do you handle hex values larger than 2⁵³ in JavaScript?
Use BigInt("0x" + hexString) for exact conversion of values above 2⁵³ − 1. parseInt and Number lose precision above 1FFFFFFFFFFFFF, which means any hex string longer than 14 digits. BigInt has no size limit.
Does parseInt accept the 0x prefix?
Yes, parseInt("0xFF", 16) returns 255. The 0x prefix is optional when radix 16 is specified. Number("0xFF") also works but requires the prefix.