Engineering function

DEC2HEX function

Converts a decimal number to hexadecimal text, such as 255 to "FF". Negatives come back as 10-character two's complement.

Available in Excel 2007 and later (Excel 2003 needed the Analysis ToolPak add-in), Microsoft 365, Excel for the web, Excel for Mac

Excel 2007 and laterWorks in Google Sheets

Syntax

DEC2HEX(number, [places])
ArgumentWhat it means
numberRequiredThe decimal whole number to convert, from -549,755,813,888 to 549,755,813,887. Decimals are truncated.
placesOptionalMinimum number of characters, padded with leading zeros. Ignored for negative numbers.

Returns: Text containing hexadecimal digits 0–9 and A–F (always upper case).

How DEC2HEX works

DEC2HEX converts a decimal number to hexadecimal (base 16), where the digits run 0–9 and then A–F for ten to fifteen. Hex is everywhere in computing: web colour codes like #1E90FF, memory addresses, MAC addresses, error codes and Unicode code points. Each hex digit stands for exactly four bits, which is why it is such a compact way of writing binary data.

The function returns text in upper case. With places you can force a fixed width — two characters per colour channel, four for a Unicode code point. Negative numbers are returned as 10 hex digits in two's complement (40 bits), so -1 becomes FFFFFFFFFF; places is ignored for them. The allowed range is about ±549.7 billion, far larger than DEC2BIN.

To convert back, use HEX2DEC. For numbers above the 40-bit limit, BASE can convert up to 2^53 (=BASE(number, 16)).

Examples

1Convert a number to hex

B2fx=DEC2HEX(A2)
AB
1Decimal
2255FF

Result: FF

255 = 15×16 + 15, and fifteen is written F in hex, so the result is FF.

2Build a web colour code from RGB

D2fx="#"&DEC2HEX(A2,2)&DEC2HEX(B2,2)&DEC2HEX(C2,2)
ABCD
1RedGreenBlue
230144255#1E90FF

Result: #1E90FF

Each channel is converted with places = 2 so that small values keep their leading zero (30 becomes 1E, but 5 would become 05 rather than 5). Joining them gives the dodger-blue colour code #1E90FF.

3A negative number

B2fx=DEC2HEX(A2)
AB
1Decimal
2-100FFFFFFFF9C

Result: FFFFFFFF9C

Negatives use 40-bit two's complement and always return 10 characters: 2^40 − 100 in hex is FFFFFFFF9C. HEX2DEC turns it back into -100.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 15 KB

Common errors and how to fix them

ErrorWhy it happensFix
#NUM!The number is outside ±549,755,813,888, the result needs more characters than places, or places is negative.Increase places, or use =BASE(number,16) for very large positive numbers.
#VALUE!number or places is text that is not a number.Check the inputs with ISNUMBER.

Tips

  • Hex results are always upper case; wrap in LOWER if you need #1e90ff style colour codes.
  • VBA and Windows store colours as BGR, not RGB, so a colour code read from the Color property needs its first and last byte pairs swapped.
  • For a Unicode character's code point use =DEC2HEX(UNICODE(A2), 4) — for example € gives 20AC.

Questions

Can DEC2HEX return lower-case letters?

No, it always uses A–F in capitals. Use =LOWER(DEC2HEX(A2)) if you need lower case.

Why does my negative number turn into FFFFFFFF…?

Excel shows negatives in 40-bit two's complement, the way a computer stores signed integers. HEX2DEC reads it back as the same negative number.

Google Sheets

DEC2HEX also exists in Google Sheets and works the same way for everyday use, so the examples above work there too.

DEC2HEX in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
DanishDEC.TIL.HEX
DutchDEC.N.HEX
FinnishDESHEKSA
FrenchDECHEX
GermanDEZINHEX
HungarianDEC.HEX
ItalianDECIMALE.HEX
Norwegian (Bokmål)DESTILHEKS
PolishDZIES.NA.SZESN
Portuguese (Brazil)DECAHEX
Portuguese (Portugal)DECAHEX
RussianДЕС.В.ШЕСТН
SpanishDEC.A.HEX
SwedishDEC.TILL.HEX