Decimal to Hex

Decimal in, hexadecimal out — padded to a real field width, and with the two’s-complement pattern rather than a minus sign when the number is negative.

Runs 100% in your browserNothing is uploaded to a serverFree forever

Decimal

Hexadecimal

Copied!

Converting decimal to hex by hand

Divide by 16, write down the remainder, repeat with the quotient, then read the remainders bottom to top. 47 ÷ 16 is 2 remainder 15, and 2 ÷ 16 is 0 remainder 2, so the digits are 2 and F: 2F. It is the same algorithm the converter above runs, except that it uses BigInt division so the loop stays exact for a number of any length rather than falling apart above 253.

The only thing to memorise is the top six digits, since decimal has no symbols for ten through fifteen:

The sixteen hex digits
DecimalHex
00
11
22
33
44
55
66
77
88
99
10A
11B
12C
13D
14E
15F

Padding, and why it matters more than it looks

Hex is almost always written to a fixed width, because the width is information. A byte is two hex digits, so 5 is written 05; a 16-bit word is four; a 32-bit word is eight. Writing 5 where the reader expects a byte loses the fact that the high nibble is zero, and in a colour, a register dump or a protocol field that is a real error rather than a cosmetic one. Set a width above and the output is padded for you.

The #RRGGBB in a stylesheet is the everyday case: an RGB triple of 60, 230, 136 has to become 3CE688, with each channel exactly two digits, or the colour is not the colour you asked for.

Landmark values

A handful of decimal numbers come up constantly, and knowing their hex on sight saves a conversion:

Numbers you are likely to be converting, and why
DecimalHexWhat it usually is
10Athe first decimal number that needs a letter
15Fthe largest single hex digit
1610one carry — hex's 10
10064no rounder in hex than it is in binary
1277Fthe top of a signed byte
12880the sign bit of a byte, on its own
255FFa full byte, and every channel of #ffffff
256100one byte over — needs two hex digits plus a carry
10003E8a round decimal number that is not round anywhere else
1024400a kibibyte
40961000a page of memory on most systems
327677FFFthe top of a signed 16-bit value
65535FFFFa full 16-bit word, and the highest TCP port
16777215FFFFFF24-bit colour, all channels full
21474836477FFFFFFFthe top of a signed 32-bit int
4294967295FFFFFFFFa full 32-bit word

Negative numbers do not have a natural hex form

You can write -2F and everybody will understand it, but no machine stores a number that way. In a register, a negative number is a two's-complement bit pattern, and that pattern depends on how wide the field is: −1 is FF in eight bits, FFFF in sixteen and FFFFFFFF in thirty-two. Choose a width above and type a negative decimal, and you get the pattern the machine would actually hold rather than a minus sign glued to a positive number.

Related: hex to decimal for the return trip, decimal to binary when you need the bits rather than the digits, and the full converter for every base at once.

More developer tools