DEC2HEX in Google Sheets: Formula & Examples

How-to basics · Updated November 23, 2026

The DEC2HEX function in Google Sheets converts a decimal number into its signed hexadecimal (base-16) equivalent. Its syntax is DEC2HEX(decimal_number, [significant_digits]), where the second argument is optional and controls the minimum number of digits in the result.

What DEC2HEX Does

Hexadecimal is a base-16 number system that uses digits 0-9 and letters A-F. Each hex digit represents four binary bits, which makes it a compact way to view binary data. DEC2HEX takes a decimal integer you provide and returns the hex representation as text. For example, DEC2HEX(255) returns FF, and DEC2HEX(16) returns 10.

The function is part of Google Sheets’ Engineering category, alongside other number-system converters like DEC2BIN, DEC2OCT, HEX2DEC, and BIN2DEC. These are useful for programmers, network engineers, and anyone working with low-level data formats.

Syntax and Arguments Explained

The function expects two arguments, though only the first is required:

  • decimal_number – the number to convert. It must be an integer between -2,147,483,648 and 2,147,483,647 (the range of a 32-bit signed integer). If you enter a decimal with a fractional part, Google Sheets truncates it toward zero (e.g., 10.9 becomes 10).
  • significant_digits – optional. The minimum number of digits to display. If the result is shorter, leading zeros are added. If you omit it, the function uses the minimum digits needed. If you provide a value, it must be between 1 and 10, and the decimal number must be non-negative (0 to 2,147,483,647).

If the decimal number is negative, the function returns a 10-character signed hexadecimal value (two’s complement). For instance, DEC2HEX(-1) returns FFFFFFFFFFFFFFFF? Actually, no – Google Sheets returns FFFFFFFFFF (10 F’s). The two’s complement representation of -1 in 32-bit is all 1s, which is 8 hex pairs, but Google Sheets pads to 10 digits for negatives.

How to Use DEC2HEX in a Sheet

To use it, type the formula directly into a cell. Here are concrete examples assuming you type the formula in cell A1:

  1. In cell A1, enter =DEC2HEX(255) – result: FF
  2. In cell A2, enter =DEC2HEX(255, 4) – result: 00FF (adds two leading zeros)
  3. In cell A3, enter =DEC2HEX(-1) – result: FFFFFFFFFF (10 characters)
  4. In cell A4, enter =DEC2HEX(16, 2) – result: 10 (no padding needed)

You can also reference a cell containing the decimal. For example, if B1 contains 100, then =DEC2HEX(B1) returns 64.

Common Errors and How to Fix Them

Users often run into a few specific errors:

  • #NUM! – This appears when the decimal number is outside the allowed range, or when significant_digits is not between 1 and 10, or when you use significant_digits with a negative decimal.
  • #VALUE! – This occurs if the decimal_number is not a valid number (e.g., text like “abc”) or if significant_digits is not a number.
  • Unexpected long results – For negative numbers, the result is always 10 characters. If you expected a shorter representation, remember that negative numbers use signed two’s complement format.

To avoid errors, ensure your decimal is an integer within the 32-bit range. If you need to convert a larger number, consider breaking it into parts or using a custom script.

DEC2HEX vs. Other Conversion Functions

Google Sheets offers several related functions. Here’s how they compare:

  • DEC2BIN – converts decimal to binary (base-2). Useful for bit-level work.
  • DEC2OCT – converts decimal to octal (base-8).
  • HEX2DEC – the reverse: converts hexadecimal back to decimal.
  • BIN2HEX and OCT2HEX – convert binary or octal to hex directly, without an intermediate decimal step.

If you need to convert a number to hex but also want to control formatting (like adding a “0x” prefix), you can use CONCATENATE or the & operator: ="0x"&DEC2HEX(255) returns 0xFF.

Real-World Use Cases

DEC2HEX is not just a classroom exercise. It appears in real workflows:

  • Color codes – Web designers often convert RGB values (0-255) to hex color codes. For example, red = 255, green = 128, blue = 0 becomes #FF8000.
  • MAC addresses – Network administrators may convert decimal IP components or interface indices to hex for certain configuration files.
  • Memory addresses – Programmers debugging embedded systems might convert decimal offsets to hex to match documentation.
  • Data validation – When importing data that uses hex identifiers, you can convert decimal IDs to hex for consistency.

For color conversion, you can combine DEC2HEX with other functions to pad single-digit results. For instance, to convert a red value in cell A1 to a two-digit hex string, use =DEC2HEX(A1, 2). Then join the three components with &.

Frequently Asked Questions

Can DEC2HEX handle negative numbers?

Yes, but the result is a 10-character signed hexadecimal number using two’s complement representation. For example, DEC2HEX(-1) returns FFFFFFFFFF. This is standard for 32-bit signed integers.

Why does DEC2HEX return #NUM! for 2147483648?

Because 2,147,483,648 is one more than the maximum allowed positive value (2,147,483,647). The function is limited to 32-bit signed integers. If you need larger numbers, consider splitting them or using a custom Google Apps Script.

How do I add leading zeros to the hex result?

Use the optional significant_digits argument. For example, =DEC2HEX(255, 4) returns 00FF. The number must be between 1 and 10, and the decimal must be non-negative.

Does DEC2HEX work in Excel?

Yes, Excel has the same function with identical syntax. However, Excel’s DEC2HEX also requires the Analysis ToolPak add-in in older versions, but in modern Excel (2013 and later) it’s built-in. The behavior is essentially the same.

Can I convert a hex number back to decimal?

Yes, use the HEX2DEC function. For example, =HEX2DEC("FF") returns 255. Note that HEX2DEC also handles signed values for hex strings up to 10 characters.

What if my decimal number has a decimal point?

Google Sheets truncates the fractional part toward zero. So DEC2HEX(10.9) returns A (since 10.9 becomes 10). No rounding occurs.

Alternatives and Limitations

If DEC2HEX doesn’t meet your needs, consider these alternatives:

  • Custom formula with BASE – The BASE function can convert to any base (2-36), so =BASE(255, 16) returns FF. However, BASE does not handle negative numbers or provide signed output.
  • Google Apps Script – For numbers beyond the 32-bit range, you can write a custom function using JavaScript’s toString(16) method, but that returns a string without padding and handles negatives differently.
  • Manual conversion – For one-off conversions, you can use a calculator or online tool, but that’s not practical for large datasets.

Remember that DEC2HEX returns text, not a number. If you need to perform arithmetic on the result, you’ll need to convert it back with HEX2DEC first.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.