Excel does not have a custom number format that turns a decimal value into hexadecimal. In Microsoft 365 for Windows, use =DEC2HEX(A2) to convert the number in A2; use the optional second argument to pad the result to a chosen width, as in =DEC2HEX(A2,6).
What “hex number format” means in Excel
Hexadecimal, or hex, is a base-16 way to write numbers. It uses the digits 0–9 and the letters A–F, where A represents 10, B represents 11, and F represents 15. For example, decimal 255 is hex FF. Hex is common in programming, color values, hardware addresses, and other settings where a compact representation of a number is useful.
In Excel, “number format” has a specific meaning: it controls how an existing numeric value is displayed. Formats such as Currency, Percentage, and custom formats can add symbols, commas, or decimal places, but they do not change the number’s base. A custom format such as 0 cannot make decimal 255 display as FF. To convert the value, use an engineering function such as DEC2HEX. Its result is text, rather than a numeric value merely wearing a hex display format.
This difference matters if you plan to calculate with the result. Keep the original decimal number in its own cell when you need arithmetic, and use a separate formula cell for its hexadecimal representation. The text output looks like a number in another base, but Excel does not treat it as an ordinary decimal number.
Convert a decimal number to hexadecimal
For a basic conversion, put a decimal integer in A2 and enter =DEC2HEX(A2) in another cell. If A2 contains 255, the formula returns FF. DEC2HEX is included in current desktop Excel; you do not need to enable the Analysis ToolPak in Microsoft 365. The function converts the numeric input and returns uppercase hexadecimal letters.
The optional second argument, called places, specifies a minimum number of characters. With decimal 255 in A2, =DEC2HEX(A2,6) returns 0000FF. This is useful when a file, device, or specification expects every value to have the same width. The extra zeroes are part of the returned text, not a display effect from cell formatting.
Use these steps in Excel for Microsoft 365 on Windows:
- Enter the decimal integer in a worksheet cell, such as A2.
- Select the cell where you want the hex text to appear.
- Type
=DEC2HEX(A2)for a standard result, or include a width such as=DEC2HEX(A2,6). - Press Enter, then fill the formula down to convert additional rows.
On Mac and in Excel for the web, the same DEC2HEX formula works. The exact ribbon layout can differ by platform, but no special number-format setting is needed to perform the conversion.
Choose the right width and handle leading zeroes
Hex values do not automatically have a fixed length. Decimal 15 converts to F, while decimal 16 converts to 10. If a downstream system expects two characters per byte, for example, F may need to appear as 0F. Set the places argument to the required minimum width: with decimal 15 in A2, =DEC2HEX(A2,2) returns 0F.
Think about the required representation before choosing a width. A six-character color value needs six hex characters; a single byte normally needs two. The places argument pads shorter positive results with leading zeroes, but it does not truncate longer results. If the requested width is too small for the converted value, Excel returns #NUM!. For example, a value that needs three hex digits cannot fit in two places.
- For a byte from 0 through 255, use two places.
- For a 24-bit value, use six places if the receiving format requires a fixed-width string.
- For negative values, DEC2HEX returns a 10-character two’s-complement representation; the places argument does not shorten that representation.
Do not rely on a custom number format to preserve zeroes in a hex string. Once a formula produces text such as 0000FF, those zeroes are characters in the result. If you type a hex code directly, Excel may interpret an entry as a number or alter it during import. Set the destination cells to Text before entering or importing codes, or prefix a manually entered value with an apostrophe. The apostrophe tells Excel to store the entry as text and is not displayed as part of the cell value.
Convert hexadecimal text back to decimal
To reverse the conversion, use HEX2DEC. If B2 contains the text FF, enter =HEX2DEC(B2) in another cell; the result is 255. HEX2DEC is useful when a worksheet receives codes from a configuration file, a programming task, or a data export and you need to compare or calculate with their decimal values.
HEX2DEC accepts hexadecimal text with up to 10 characters. A 10-character value is interpreted as a signed 40-bit two’s-complement number, so =HEX2DEC("FFFFFFFFFF") returns -1. In ordinary positive cases, the function reads the digits as a base-16 value. If the input contains invalid characters, is too long, or falls outside the function’s supported range, Excel can return an error. Keep imported codes as text so Excel does not remove leading zeroes before the conversion.
For a round trip, place a decimal integer in A2, convert it with =DEC2HEX(A2), and convert that result back with =HEX2DEC(B2) if the hex result is in B2. For supported values, the final decimal result matches the original. If you use a negative input, remember that its hex text represents the signed 40-bit form; it is not a minus sign followed by an ordinary positive hex magnitude.
Use hex values for colors
A frequent reason to ask about Excel hex formatting is to create or read color codes. A common six-digit web color is written as RRGGBB: two hex digits for red, two for green, and two for blue. Each channel ranges from 0 to 255, so each pair fits in one byte. Excel’s DEC2HEX function can convert each channel, but it does not itself apply a color to a cell.
Suppose red, green, and blue channel values are in A2, B2, and C2, respectively. To build a six-character color code, use =DEC2HEX(A2,2)&DEC2HEX(B2,2)&DEC2HEX(C2,2). If the channels are 255, 0, and 128, the result is FF0080. Each call specifies two places so that a channel such as 5 becomes 05 rather than 5. The ampersands join the three text results into one code.
Hex color notation can vary by context: some formats use a leading #, and some systems use an additional alpha channel for transparency. Add those characters only when the destination expects them. For example, to include a # before the assembled code, use ="#"&DEC2HEX(A2,2)&DEC2HEX(B2,2)&DEC2HEX(C2,2). This creates text; it does not automatically change Excel’s fill color. To apply a color, use Excel’s fill-color controls or an appropriate conditional-formatting rule separately.
Limits, errors, and common misconceptions
DEC2HEX supports decimal values from -549755813888 through 549755813887, the signed range for 40 bits. The function is intended for integer conversion; do not use it as a way to display fractional values in another base. Values outside the supported range, a places value that is invalid, or a width that is too short can produce #NUM!. An input Excel cannot interpret as a number can produce #VALUE!.
Excel stores numeric values with limited precision, so very long decimal identifiers should not be treated as ordinary numbers. If an identifier has more digits than Excel can preserve exactly, import or store it as text instead. This is especially important for codes that are identifiers rather than quantities: arithmetic precision is irrelevant to an identifier, but changing even one digit can make it unusable.
Another common misconception is that changing a cell to Text converts a decimal number into hexadecimal. It does not. The Text format affects how entries are interpreted and stored; it does not perform a base conversion. Likewise, changing a cell’s format after a number has already been entered will not convert the existing value. Use DEC2HEX for conversion, and use Text when you need to preserve literal codes or leading zeroes.
When to use a formula, text, or a custom format
Choose the method based on what the cell represents:
- Use DEC2HEX when the cell contains a decimal quantity and you need its actual hexadecimal representation for a file, calculation workflow, or reference.
- Use Text when the cell contains a code that should remain exactly as entered, such as a hex value imported from another system.
- Use a custom number format for visual changes to decimal values, such as adding leading zeroes to a decimal ID or displaying a unit. It cannot convert decimal digits into hex digits.
For a workbook that must preserve both meanings, keep the source number and its conversion in separate columns. Label them clearly, for example Decimal and Hex, so another reader does not mistake the text result for a decimal value. If you export the hex column, check that the receiving system expects uppercase or lowercase letters, fixed width, and any required prefix.
Frequently Asked Questions
Can I display a number in hex with a custom Excel format?
No. Excel custom number formats change the display of decimal values but do not convert their base. Use DEC2HEX to return the hexadecimal text.
How do I show leading zeroes in a hex result?
Set the places argument in DEC2HEX. For example, with decimal 15 in A2, =DEC2HEX(A2,2) returns 0F.
Does DEC2HEX return a number or text?
It returns text containing hexadecimal characters. Keep the original decimal value in a separate cell if you need to perform arithmetic with it.
How do I convert hex back to decimal in Excel?
Put the hex characters in B2 and use =HEX2DEC(B2). For example, FF returns 255.
Why does DEC2HEX return #NUM!?
Common causes include a decimal input outside the function’s supported range, a places argument that is invalid, or a requested width too small for the result. Check the input and the required number of hex digits.
Related
- Excel Employee Work Schedule: Setup and Tips
- Excel EIN Number Format: Keep All 9 Digits Visible
- Excel Dependent Drop-Down Lists: Setup and Fixes
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Share a Calendar in Outlook: Steps and PermissionsTo share a calendar in Outlook, open Calendar, choose the calendar you want to share, select Share or…
- Excel Shortcut to Insert Cells Down: Keyboard StepsIn Excel for Microsoft 365 on Windows, select the cell or range where you want space, then press…
- How to Archive Emails in New Outlook 365 Without DeletingTo archive emails in new Outlook 365, select the message or messages and choose Archive on the toolbar.…
- CSV vs Excel File Size: Why the Difference MattersCSV files are usually smaller than Excel workbooks when they contain the same plain table of data, because…
- Outlook Email Preview: Change Lines and Reading PaneTo change the email preview in Outlook, adjust the message-preview lines to show more or less text in…
- Outlook Calendar Share Request: How to Send and Manage OneTo share your Outlook calendar, open Calendar, choose Share Calendar or Share, enter the recipient, set their permission…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.