Hex to Decimal

Hex to decimal in Excel

Type a hex value and pick a formula. The result shows what Excel returns, errors included.

HEX2DEC

Output:

HEX2DEC function

Type =HEX2DEC("FF") in a cell to convert the hex string FF to 255. HEX2DEC is an engineering function available in Excel 2007 and later, Google Sheets, and LibreOffice Calc.

=HEX2DEC("1A3F")     → 6719
=HEX2DEC("ff")        → 255 (case does not matter)
=HEX2DEC(A2)          → converts the hex string in cell A2

Convert a column of hex values

  1. Put the hex values in column A, starting at A2.
  2. In B2, type =HEX2DEC(A2) and press Enter.
  3. Select B2, drag the fill handle down to the last row, and every row converts.

HEX2DEC accepts the hex string directly in quotes or as a cell reference. The input must contain only the digits 0–9 and letters A–F. Any prefix, space or other character returns #NUM!. Format the input column as Text, because Excel turns a value such as 1E5 into a number and drops leading zeros.

The 10-digit limit and negative results

HEX2DEC accepts at most 10 hex digits (40 bits) and reads values from 8000000000 to FFFFFFFFFF as negative. This 40-bit two's complement rule means FFFFFFFFFF returns −1, not 1,099,511,627,775.

FormulaResultWhy
=HEX2DEC("FF")2552 digits, positive
=HEX2DEC("FFFFFFFF")4,294,967,2958 digits, positive (top bit is 0 in the 40-bit view)
=HEX2DEC("FFFFFFFFFF")−110 digits, top bit is 1, so negative
=HEX2DEC("1FFFFFFFFFF")#NUM!11 digits, too long

Get the unsigned value from a 10-digit input

When HEX2DEC returns a negative number but the value is unsigned, add 2⁴⁰:

=IF(HEX2DEC(A2)<0, HEX2DEC(A2) + 2^40, HEX2DEC(A2))

FFFFFFFFFF then returns 1,099,511,627,775 instead of −1.

DECIMAL function

=DECIMAL("FF", 16) converts hex to decimal without the 10-digit limit. DECIMAL is available in Excel 2013 and later, and in Google Sheets. Its second argument is the base, so 16 means hex.

=DECIMAL("1A3F", 16)                    → 6719
=DECIMAL("FFFFFFFFFF", 16)              → 1099511627775
=DECIMAL("FFFFFFFFFFFFFFFFFFFF", 16)    → about 1.20893E+24 (rounded, see below)

DECIMAL accepts strings up to 255 characters and always returns a positive value, because it has no signed two's complement rule. The result is a standard Excel number, so values above 2⁵³ − 1 (9,007,199,254,740,991) lose precision. Excel stores numbers as 64-bit IEEE 754 floats with about 15 significant digits, and the cell shows the long example above as 1.20893E+24.

Strip 0x and # prefixes

HEX2DEC and DECIMAL return #NUM! when the input contains a 0x or # prefix. Remove the prefix with SUBSTITUTE or MID before passing the string.

=HEX2DEC(SUBSTITUTE(A2, "0x", ""))
=HEX2DEC(SUBSTITUTE(A2, "#", ""))

For cells that may carry either prefix:

=HEX2DEC(SUBSTITUTE(SUBSTITUTE(A2, "0x", ""), "#", ""))

When the prefix length is fixed (2 characters for 0x, 1 for #), MID is an alternative:

=HEX2DEC(MID(A2, 3, LEN(A2)-2))

Signed conversion at different bit widths

HEX2DEC reads 10-digit values as signed at 40 bits, but hardware and protocols use 8, 16, 32 or 64-bit widths. To read a hex value as a signed integer at a specific width, use this formula pattern:

=MOD(HEX2DEC(A2) + 2^(bits-1), 2^bits) - 2^(bits-1)

The formula adds half the range, wraps with MOD, then subtracts half the range. The result matches two's complement at the chosen width. A dash marks a value that is wider than the width.

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

WidthFormulaFFFF9CFFFFFFFF
8-bit=MOD(HEX2DEC(A2)+128, 256)-128−1——
16-bit=MOD(HEX2DEC(A2)+32768, 65536)-32768255−100—
32-bit=MOD(HEX2DEC(A2)+2^31, 2^32)-2^3125565436−1

VBA: hex to decimal in a macro

Visual Basic for Applications (VBA) converts hex with Val("&H" & hexString). The &H prefix tells VBA the string is hexadecimal.

Function HexToDec(hexStr As String) As Double
    hexStr = Replace(hexStr, "0x", "")
    hexStr = Replace(hexStr, "#", "")
    HexToDec = Val("&H" & hexStr & "&")
End Function

The trailing & after the hex string forces VBA to use a Long instead of an Integer, which avoids a wrong result for values above 7FFF. Call the function from a cell as =HexToDec(A2). Values from 80000000 to FFFFFFFF come back negative, because a VBA Long is a signed 32-bit number.

For strings longer than 8 hex digits, Val does not work. Call the DECIMAL worksheet function from VBA instead:

Application.WorksheetFunction.Decimal(hexStr, 16)

Google Sheets

Google Sheets has the same HEX2DEC and DECIMAL functions with the same syntax. =HEX2DEC("FF") returns 255, and =DECIMAL("FF", 16) returns 255. The 10-digit limit and the 40-bit signed rule apply to HEX2DEC in Google Sheets too.

Both functions work in every Sheets file without an add-in, and the formulas on this page carry over unchanged.

Decimal to hex with DEC2HEX

=DEC2HEX(255) returns "FF". A second argument pads the result to a fixed width: =DEC2HEX(255, 4) returns "00FF".

=DEC2HEX(255)         → FF
=DEC2HEX(255, 4)      → 00FF
=DEC2HEX(-1)          → FFFFFFFFFF (negative numbers ignore the places argument)
=DEC2HEX(255, 2)      → FF

DEC2HEX accepts values from −549,755,813,888 to 549,755,813,887 (the signed 40-bit range) and returns #NUM! outside it. For a larger non-negative whole number, use =BASE(A2, 16) (Excel 2013 and later, and Google Sheets).

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

Method comparison

MethodMax inputSignedAccepts 0xAvailable in
HEX2DEC(s)10 hex digitsYes (40-bit)NoExcel 2007+, Sheets, Calc
DECIMAL(s, 16)255 charactersNoNoExcel 2013+, Sheets
VBA Val("&H" & s & "&")8 hex digitsYes (32-bit)NoExcel desktop
VBA WorksheetFunction.Decimal255 charactersNoNoExcel desktop

Start with HEX2DEC for values up to 10 hex digits. Switch to DECIMAL(s, 16) for longer strings, and use the MOD formula when the value is signed at a specific bit width.

Try every method on one string

Type one string and see which formula returns a number.

Frequently asked questions

How do you convert hex to decimal in Excel?
Type =HEX2DEC("FF") in a cell to get 255. HEX2DEC takes a hex string (or a cell reference) and returns its decimal value. It works in Excel, Google Sheets, and LibreOffice Calc.
Why does HEX2DEC return a negative number?
HEX2DEC reads 10-digit hex values as 40-bit signed two's complement. FFFFFFFFFF returns −1 because the top bit is 1. Values with 9 or fewer digits are always positive. To get the unsigned value, add 2⁴⁰ when the result is negative.
How do you convert hex values longer than 10 digits?
Use =DECIMAL(A2, 16) instead of HEX2DEC. DECIMAL accepts up to 255 characters and always returns a positive result. Values above 2⁵³ − 1 lose precision because Excel stores numbers as 64-bit floats.
How do you strip the 0x prefix before converting?
Wrap the input in SUBSTITUTE: =HEX2DEC(SUBSTITUTE(A2, "0x", "")). For the # prefix used in CSS colour codes, replace "#" instead. Nest both SUBSTITUTE calls to handle either prefix. SUBSTITUTE is case-sensitive, so a capital 0X needs its own call.
Does HEX2DEC work in Google Sheets?
Yes, with the same syntax and the same 10-digit limit. =HEX2DEC("FF") returns 255 in Google Sheets. =DECIMAL("FF", 16) also works for longer values.
How do you read signed 32-bit hex in Excel?
Use =MOD(HEX2DEC(A2)+2^31, 2^32)-2^31. This formula reads the hex value as a signed 32-bit integer. FFFFFFFF returns −1, and 80000000 returns −2,147,483,648. Replace 31 and 32 with 7 and 8 for 8-bit, or 15 and 16 for 16-bit.