How to convert a number to hex or binary
✓ Verified in LibreOffice 25.8.7.3 ✓ Verified in Google Sheets (2026-08-30)Turn a decimal number into its hexadecimal, binary, or octal text — for color codes, bitmasks, memory addresses, and low-level debugging.
The formula
| App | Formula | Notes |
|---|---|---|
| Excel (desktop) | =DEC2HEX(A2) | DEC2HEX for hexadecimal, DEC2BIN for binary, DEC2OCT for octal. Add a second argument to pad with leading zeros, e.g. =DEC2HEX(A2,4). |
| Google Sheets | =DEC2HEX(A2) | Identical. All three DEC2* functions exist in Sheets. |
| LibreOffice Calc | =DEC2HEX(A2) | Identical. |
How it works
The DEC2* family converts a decimal whole number into another base as TEXT: DEC2HEX(255) is "FF", DEC2BIN(10) is "1010", and DEC2OCT(8) is "10". Each takes an optional second 'places' argument that left-pads the result with zeros to a fixed width — handy for lining up byte values, e.g. DEC2HEX(15,2) gives "0F". They cap out at fairly small magnitudes (DEC2BIN only handles -512..511), so for large numbers use BASE(number, radix) instead, which converts to any base from 2 to 36. Because the output is text, don't do further math on it directly. To go the other way, HEX2DEC, BIN2DEC and OCT2DEC convert back to a number, and functions like HEX2BIN convert straight between non-decimal bases. A common use is building a hex colour string: "#"&DEC2HEX(r,2)&DEC2HEX(g,2)&DEC2HEX(b,2).
Verified, not just documented
We ran =DEC2HEX(255) in LibreOffice 25.8.7.3 (headless, with forced recalculation) and it returned FF — exactly the expected result. We then ran the same formulas in Google Sheets, executed 2026-08-30: a formula-only workbook goes into Google Drive, which converts it to a Sheet and recalculates every formula with Google’s own engine, and comes back out as .xlsx carrying the values Google computed. It returned FF for the worked example, the same value LibreOffice produced. Both engines’ numbers on this page are executed results. The Excel formula follows Microsoft’s official documented syntax — we do not run desktop Excel.
Functions used
DEC2HEX — see full Excel, Google Sheets & LibreOffice compatibility for each.