Contact

DEC2HEX in Google Sheets: Fixed-Width Hex and Safe Arithmetic

Engineering Intermediate Google Sheets

Create hexadecimal fields in Google Sheets, distinguish numeric inputs from encoded outputs and reproduce signed forty-bit and color-code examples.

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

Quick answer: DEC2HEX(decimal_number,[significant_digits]) converts a decimal quantity to signed hexadecimal notation. Optional significant digits pads the output on the left; it does not change the number or make ordinary arithmetic hexadecimal-aware.

Availability and version history

  • Introduced: first introduction date is not established by the reviewed official history; no desktop release number is inferred.
  • Availability: documented in the rolling Google Sheets service; these examples use the web worksheet workflow.
  • Checked: October 14, 2026; official DEC2HEX reference. Documentation and independent models, with no native Sheets execution or wider client certification.

Syntax and documented range

=DEC2HEX(decimal_number,[significant_digits])

Google documents decimal inputs from −549755813888 through 549755813887. A valid number is automatically converted to the expected string input: numeric 100 and text “100” both produce 64.

The optional second argument adds leading zeros when the required width is smaller. An insufficient width produces #NUM!. Google says the width is ignored for negative inputs, but the short reference does not separately resolve every invalid width, fractional input or native error interaction.

The signed forty-bit interpretation is described in Google’s HEX2DEC reference. The negative examples below follow that convention and are independently computed; no different product’s coercion rules are assumed.

Example 1: construct a two-character field

Enter 0, 9, 31, 128 and 240 in A2:A6. In B2 enter and fill through B6:

=DEC2HEX(A2,2)
Decimal quantity Expected hexadecimal field
0 00
9 09
31 1F
128 80
240 F0

The letters A–F represent digit values 10–15. Thus 1F means 1×16+15=31, and F0 means 15×16=240. Leading zeros maintain the two-character layout.

In C2 use =HEX2DEC(B2) and fill down. The expected numbers return to 0, 9, 31, 128 and 240. HEX2DEC explicitly interprets the representation rather than treating digit-only codes as ordinary decimal quantities.

Under Google’s documented input conversion, =DEC2HEX(31,2) and =DEC2HEX("31",2) both have independently expected output 1F. The input string contains decimal digits; the output uses hexadecimal notation.

Example 2: avoid arithmetic on an encoded string

Put =DEC2HEX(512) in E2. The output is 200, since 2×16^2=512. To add decimal 16 to the original quantity, use:

=HEX2DEC(E2)+16

The result is 528. Re-encoding with =DEC2HEX(HEX2DEC(E2)+16) produces 210, since 2×256+1×16=528.

Google warns that digit-only hexadecimal results can silently participate in decimal arithmetic. An ordinary =E2+16 instead interprets the displayed digits 200 as decimal and gives 216. That is not the intended hexadecimal representation 210.

Keep the numeric quantity and its encoded label in different columns. A representation that happens to contain only digits is still not safe to use as if it were the original decimal measurement.

Advanced example: assemble a three-channel label

Define three integer channels from 0 through 255. Enter 64, 128 and 9 in G2:I2. Use:

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

The expected label is #408009. The final channel needs its leading zero to preserve the six-character field layout.

An independent packed-value check is =G2*65536+H2*256+I2, yielding 4227081. =DEC2HEX(4227081,6) returns 408009. The string’s leading # is a presentation prefix and is not part of the hexadecimal number.

For channels 9, 0 and 240, the corresponding label is #0900F0. This example creates a string; it does not change a cell’s actual fill color or execute an external workflow.

Advanced check: signed width changes interpretation

The signed forty-bit model gives these expected conversions:

Decimal Hexadecimal
-1 FFFFFFFFFF
-16 FFFFFFFFF0
-549755813888 8000000000
549755813887 7FFFFFFFFF

For −16, add 2^40 to obtain its unsigned forty-bit encoding, then express that number in ten hex positions. The short code F0 represents positive 240; it is not the same thing as FFFFFFFFF0.

A valid supplied width such as 2 is ignored for the negative input. Do not infer a signed-byte format from that width argument. The format’s signed interval and its output padding are separate rules.

Validation and remaining limits

=DEC2HEX(31,1) needs two characters and follows the documented insufficient-width #NUM! rule. Width 4 produces 001F. The page does not establish a general fractional-width truncation rule, so integer widths are used throughout.

DECIMAL and BASE offer different radix workflows under their own constraints. Native blank/error coercion, overflow details and undocumented invalid-width combinations remain outside this documentation-and-model review.

References and verification

Examples use fictional data, English function names and comma separators. Expected outputs are independent mathematical calculations or explicitly documented rules, not native Sheets captures. Check the spreadsheet locale when reproducing punctuation-sensitive formulas.