Convert decimal numbers to hexadecimal
A practical review of decimal-to-hexadecimal conversion: what it changes in stored text, when hex is suitable, and how the result behaves when copied, searched, or read aloud.
Decimal-to-hexadecimal conversion rewrites a whole number from base 10 into base 16. The numeric value is meant to stay the same; the stored digit characters change. Use this review to judge whether that rewrite still preserves meaning, readability, and practical usability for your workflow.
Is this transformation suitable for your intended use?
Hexadecimal is a good fit when the number is a bit pattern, byte, memory address, color channel, hash fragment, or other machine-oriented quantity that people already expect in base 16. The main benefit is a compact, lossless mapping for non-negative integers, using sixteen digits instead of ten, so each hex digit covers four bits and two hex digits cover one byte.
The decisive tradeoff is interpretation. The same character sequence can name different values once the radix changes: decimal 10 is ten, while hexadecimal 10 is sixteen. Anyone or any tool that still treats the text as decimal will misread it. Keep the original decimal form when the number is a count, price, measurement, identifier spoken aloud, or any figure meant for people who have no reason to apply a radix convention.
- Prefer hexadecimal for bitwise work, byte dumps, CSS-style color channels, machine addresses, and encoded binary shown as text.
- Prefer decimal for quantities people compare or enter without a programming context.
- Keep both forms when a log, API, or UI must serve programmers and non-programmers at once.
- Treat signed values, fixed bit widths, and an 0x prefix as separate choices, not as automatic parts of “convert to hex.”
What decimal-to-hex conversion actually changes
The conversion changes the stored characters of the number. Digit characters 0–9 in a base-10 spelling are replaced by digit characters 0–9 and A–F (or a–f) in a base-16 spelling. That is a real edit to the text, not a font, not CSS, and not a bidirectional or mirrored display. Screen appearance follows the new characters; there is no separate “hex look” layered over the original decimal string.
This is unrelated to character reversal, word reversal, mirrored glyphs, or Unicode substitution used for decorative reverse or mirror text. Combining marks and multi-code-point emoji do not arise in a well-formed decimal integer. If the input is free text rather than a number, decimal-to-hex conversion does not apply until you first parse a numeric value.
Each hex digit stands for a value from 0 through 15. Letters A–F (or a–f) stand for 10 through 15. Optional decoration is separate from the value: a leading 0x or 0X marks hex notation in many languages; padding with zeros marks a fixed width; uppercase versus lowercase is a spelling choice that does not change the number.
| Decimal value | Hex digits (no prefix) | Common prefixed form |
| 0 | 0 | 0x0 |
| 10 | a or A | 0xa |
| 15 | f or F | 0xf |
| 16 | 10 | 0x10 |
| 255 | ff or FF | 0xff |
| 256 | 100 | 0x100 |
The usual hand method is repeated division by 16. Remainders, written from last to first, become the hex digits. Remainder 10 is A, 11 is B, 12 is C, 13 is D, 14 is E, and 15 is F. Multiplying each hex digit by the matching power of 16 and summing recovers the decimal value, which is the check that meaning was preserved.
Python, and the same conversion in nearby languages
In Python the built-in hex function returns a string with a 0x prefix and lowercase letters. format or an f-string gives control over prefix, width, and case. Python integers are signed and unbounded, so hex of a negative value keeps a minus sign in front of the 0x form rather than emitting a two’s-complement bit pattern unless you mask the value to a fixed width first.
hex(255) # '0xff'
format(255, 'x') # 'ff'
format(255, 'X') # 'FF'
f'{255:#x}' # '0xff'
hex(-42) # '-0x2a'
format(42 & 0xFF, '02x') # 8-bit unsigned style: '2a'
format((-42) & 0xFFFFFFFF, '08x') # 32-bit two's-complement style
“Unsigned hex” is a width and interpretation choice. Masking with 0xFF, 0xFFFF, or 0xFFFFFFFF selects 8-, 16-, or 32-bit wraparound and then formats those bits as hex. Without a mask, Python reports the mathematical integer, including a leading minus for negatives. Confirm which of those two meanings your caller wants before you treat the strings as interchangeable.
JavaScript numbers expose toString(16) for the same radix change. In C, printf or snprintf with %x or %X writes lowercase or uppercase hex; the length modifiers and unsigned types decide how a negative stored value is shown. In R, hexmode values and sprintf with %x play the same role. Online converters follow the same digit mapping when they document integer input; this review does not verify any named web tool’s options for width, sign, or prefix, so treat those as unspecified until you read that tool’s own help.
JavaScript: (255).toString(16) // "ff"
C: printf("%x\n", 255u); // ff
R: sprintf("%x", 255) // "ff"
Will the result stay readable and usable after copying or sharing?
A hexadecimal integer written with ASCII digits 0–9 and letters A–F or a–f, with or without a 0x prefix, is ordinary plain text. Copy and paste preserve those characters in typical editors, terminals, emails, and source files. No special Unicode styling is required, and no plain-text equivalent is needed beyond keeping the original decimal spelling if readers still need it.
Search and exact-match editing follow the stored spelling. A find for 255 will miss ff and 0xff. A find for 10 may hit both decimal ten and hex sixteen if the surrounding text does not mark the radix. Downstream parsers that expect a decimal integer will reject or misread hex letters. If a document, ticket, or API must remain searchable by the human-facing value, store the decimal form as well, or label the hex string so the radix is visible.
Assistive technology will generally speak hex as letters and digits: ff as F F, or 0xff in whatever way a given voice engine handles the prefix. That reading order is the left-to-right character order of the string; the conversion does not reverse or mirror glyphs. This article does not include a scoped test of a named screen reader, so treat pronunciation of 0x, mixed case, and long padded strings as environment-specific. For spoken or printed instructions aimed at a general audience, pair the hex form with the decimal value.
- Copy the hex string as text; it remains editable ASCII in ordinary destinations.
- Label the radix (hex, 0x, or similar) whenever the same digit length could be read as decimal.
- Keep a decimal spelling next to hex when search, forms, or non-technical readers still need the original number.
- Agree on case, prefix, and bit width before comparing hex strings for equality.
- Do not assume an online converter’s sign, width, or prefix rules without that converter’s documentation.
If the goal is only to make a number look decorative, reversed, or mirrored, hexadecimal conversion is the wrong tool: it changes the number’s written radix. If the goal is to carry the same integer in a form that maps cleanly onto bits and bytes, hexadecimal is suitable, provided you keep sign, width, and prefix explicit and leave a decimal form wherever people still need to read the value without a radix key.