How to convert a number to hex or binary
✓ Verified in LibreOffice 25.8.7.3Turn 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 | =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. Every formula here is confirmed by actually executing it.
Functions used
DEC2HEX — see full Excel, Google Sheets & LibreOffice compatibility for each.