Contact

DEC2HEX in Excel: Pad Hex Codes and Decode Signed Width

Engineering Intermediate Microsoft Excel

Create hexadecimal codes from decimal integers in Excel, pad fixed-width fields and distinguish positive byte values from signed forty-bit results.

Verification status: See the evidence and test scope described in this guide. How formula examples are checked

Quick answer: DEC2HEX(number,[places]) converts a decimal integer to hexadecimal notation. Optional places pads nonnegative results with leading zeros; negative values use ten hexadecimal characters representing a forty-bit two’s-complement value.

Availability and version history

  • Introduced: the first introduction version is not established by the reviewed official history.
  • Availability: official help lists Excel for Microsoft 365, Excel for Microsoft 365 for Mac, Excel 2024, Excel 2024 for Mac, Excel 2021, Excel 2021 for Mac, Excel 2019, Excel 2016.
  • Checked: October 14, 2026; official DEC2HEX reference. Documentation and independent models; no native Excel execution or wider platform certification.

Syntax, range and optional places

=DEC2HEX(number,[places])

The documented number interval is −549755813888 through 549755813887, or −2^39 through 2^39−1. Values outside that interval produce #NUM!; nonnumeric number or places arguments produce #VALUE!.

When places is omitted, a nonnegative value uses the minimum necessary width. A supplied width that is too short produces #NUM!; fractional places is truncated, and negative places is invalid. The dedicated reference does not separately settle zero places or fractional-number handling, so those cases remain outside the examples.

For negative numbers, places is ignored and the representation contains ten hexadecimal characters. The examples use ordinary valid widths rather than claiming how invalid optional arguments interact with a negative number.

Example 1: create fixed-width codes

Enter 0, 15, 26, 160 and 255 in A2:A6. In B2 enter =DEC2HEX(A2) and in C2 enter =DEC2HEX(A2,2). Fill both down.

Decimal Minimum representation Two-character code
0 0 00
15 F 0F
26 1A 1A
160 A0 A0
255 FF FF

Hexadecimal digits A through F represent values 10 through 15. Thus 1A is 1×16+10=26, while A0 is 10×16=160. Padding is a representation choice and does not change the quantity.

In D2 use =HEX2DEC(C2) and fill down. The results match the original decimal values. Keep the source column for arithmetic and numeric sorting; a hexadecimal code is a representation of the number rather than its decimal display.

Example 2: understand negative values

These independently checked two’s-complement controls use the full forty-bit format:

Formula Expected representation
=DEC2HEX(-1) FFFFFFFFFF
=DEC2HEX(-2) FFFFFFFFFE
=DEC2HEX(-54) FFFFFFFFCA
=DEC2HEX(-549755813888) 8000000000
=DEC2HEX(549755813887) 7FFFFFFFFF

For a negative value, add 2^40, or 1099511627776, before expressing the resulting unsigned value in ten hexadecimal positions. This is an independent explanation of the fixed-width encoding, not a new Excel formula requirement.

=DEC2HEX(-1,2) still yields FFFFFFFFFF. It does not yield a two-character FF representation for a signed byte. Conversely, =DEC2HEX(255,2) yields FF and HEX2DEC decodes that short representation as positive 255.

Advanced example: assemble a three-channel color code

This example treats three numeric channels as a data-format exercise. Each must be an integer from 0 through 255. Enter 24, 160 and 255 in F2:H2.

="#"&DEC2HEX(F2,2)&DEC2HEX(G2,2)&DEC2HEX(H2,2)

The expected label is #18A0FF. Two-character padding is essential: without it, smaller channel values could lose their field boundaries. For 0, 15 and 255, the corresponding label is #000FFF.

An independent packed-value control is:

=F2*65536+G2*256+H2

For 24, 160 and 255 this gives 1614079. =DEC2HEX(1614079,6) returns 18A0FF. The channel layout follows positional weights 16^4, 16^2 and 1. The leading # belongs to the presentation label and is not supplied to HEX2DEC.

This exercise does not claim that the formula changes cell colors. It produces a string a compatible downstream workflow could use after its own validation.

Common errors and checks

=DEC2HEX(64,1) has the documented #NUM! result because 64 is hex 40 and needs two characters. =DEC2HEX(26,4) returns 001A; =DEC2HEX(26,4.9) uses truncated width 4 and returns the same representation.

Values beyond the signed interval are not made valid by a larger places argument. For unsigned representations with different limits, BASE is a separate function with its own contract. DEC2BIN also has a different, much smaller numeric interval; conversion-family names do not imply identical ranges.

Native coercion, extreme error precedence and undocumented width boundaries were not executed here. All worked inputs are explicit integers and use the documented small-width or signed-boundary conventions.

References and verification

Expected outputs are independently calculated examples or documented rules, not captured native Excel results. Formulas use English function names and comma argument separators. Locale, rendering and boundary restrictions are stated where relevant.