When You Need Decimal-to-Hex Conversion
Developers convert decimal to hex constantly: generating CSS color values, writing bitmask constants, reading kernel source code that uses hex literals, encoding byte values in network packets, and formatting output for debugger breakpoints. The 0x prefix is the universal signal for hex in nearly every C-family language.
The Conversion Algorithm
To convert a decimal integer to hex, repeatedly divide by 16 and record the remainders. Convert remainders 10–15 to letters A–F. Read the remainders from bottom to top.
Example: 6699 ÷ 16 = 418 remainder 11 (B). 418 ÷ 16 = 26 remainder 2. 26 ÷ 16 = 1 remainder 10 (A). 1 ÷ 16 = 0 remainder 1. Reading remainders upward: 1A2B. So 6699 decimal = 0x1A2B.
// JavaScript
(255).toString(16) // → "ff"
(255).toString(16).toUpperCase() // → "FF"
'0x' + (255).toString(16).toUpperCase() // → "0xFF"
// Python
hex(255) # → '0xff'
format(255, 'X') # → 'FF' (uppercase, no prefix)
f'{255:08X}' # → '000000FF' (zero-padded to 8 digits) Negative Numbers and Two's Complement
Negative integers in binary are stored as two's complement. The decimal value -1 in a 32-bit two's complement system is 0xFFFFFFFF (all bits set to 1). The value -128 is 0xFFFFFF80. This converter shows the 32-bit two's complement hex for negative inputs, which is what you see in a debugger inspecting a signed integer register.
Practical Uses in Web Development
CSS colors: Convert RGB channel values to hex to build a color code. R=255, G=87, B=51 → 0xFF, 0x57, 0x33 → #FF5733. The RGB to hex converter on this site does the full three-channel conversion at once.
Bitmask constants: Permission systems often define constants in hex. chmod values, TCP flags, and Linux capabilities are all defined as hex bitmasks because each hex digit represents 4 bits — a visual hint about the binary structure.
printf formatting: In C and Go, %x and %X format a decimal integer as hex. In Python, format(n, 'X') or f'{n:X}' do the same. When writing test assertions that compare hex output, you need to know what decimal values correspond to.
Frequently Asked Questions
How do I convert decimal to hex in JavaScript?
Use number.toString(16). For example, (255).toString(16) returns 'ff'. To get uppercase, chain .toUpperCase(): (255).toString(16).toUpperCase() returns 'FF'. To add the 0x prefix: '0x' + (255).toString(16).toUpperCase().
What is 255 in hex?
255 in decimal equals FF in hexadecimal. It is the maximum value of one byte (8 bits). In CSS, #FFFFFF is pure white because all three channels (R, G, B) are at their maximum value of 255 = 0xFF.
What is the hex for the decimal value 16?
16 in decimal equals 10 in hexadecimal. In base-16, the digit 1 in the second position represents 1 × 16 = 16. This is analogous to decimal: the digit 1 in the second position of '10' represents 1 × 10 = 10.
How are negative numbers represented in hex?
Negative integers in computers are stored as two's complement. In a 32-bit system, -1 is stored as 0xFFFFFFFF (all 32 bits set to 1). -128 is 0xFFFFFF80. This converter shows the 32-bit two's complement hex for negative inputs, matching what you would see in a debugger.
How do I pad a hex number to 8 digits?
In JavaScript: ('0000000' + n.toString(16)).slice(-8).toUpperCase(). In Python: format(n, '08X') or f'{n:08X}'. Zero-padded hex is common when working with fixed-width data like IPv4 addresses, color codes, or 32-bit register values.