The 3 steps
Convert RGB to HEX in 3 steps: divide each number by 16, turn the quotient and remainder into 2 hex digits, and join the 3 pairs after a hash. The value rgb(255, 87, 51) becomes #FF5733, because 255 gives FF, 87 gives 57, and 51 gives 33.
Each RGB number runs from 0 to 255, and each one becomes exactly one 2-digit hex pair. Dividing by 16 splits the number into a first digit and a second digit, because 16 is the base of hex. The quotient becomes the first digit and the remainder becomes the second digit. Do that for red, green, and blue in order, then write the 6 digits after a hash sign.
rgb(255, 87, 51)
Start with the 3 numbers, red first, green second, blue third.
Step forward and back through the conversion of rgb(255, 87, 51).
Step 1: divide by 16
Divide each RGB number by 16 and keep the whole-number quotient and the remainder. The number 87 divided by 16 is 5 with a remainder of 7, because 5 times 16 is 80 and 87 minus 80 is 7. The quotient and the remainder are your 2 hex digits.
You are splitting the number into groups of 16, which is what base 16 means. The quotient tells you how many full groups of 16 fit, and the remainder tells you what is left over. Both parts are always from 0 to 15, which is exactly the range of one hex digit. If the number is 255, the division is 15 with a remainder of 15, because 15 times 16 is 240 and 255 minus 240 is 15.
87 ÷ 16 = 5 remainder 7
Each filled block is one group of 16. The leftover is the remainder.
Step 2: turn 10 to 15 into letters
Turn any quotient or remainder from 10 to 15 into a letter: 10 is A, 11 is B, 12 is C, 13 is D, 14 is E, and 15 is F. The number 219 divided by 16 is 13 with a remainder of 11, so its pair is DB, not 13-11. Numbers 0 to 9 stay as digits.
Hex needs 16 symbols for one digit, and the digits 0 to 9 give only 10, so the letters A to F supply the other 6. This step is where most mistakes happen, because a remainder of 13 looks like it should be written as 13, but it is one digit, D. Keep the letter table beside you until you know it by heart. A pair is always 2 symbols, never 3.
13 is the letter D.
Tap a value to highlight it. Quiz: what letter is a remainder of 13? D.
Step 3: join the pairs after a hash
Join the 3 pairs in order — red, green, blue — and put a hash sign in front. The values rgb(59, 130, 246) give the pairs 3B, 82, and F6, so the answer is #3B82F6. The order is the same as the RGB numbers: red first, green second, blue third.
A missing leading zero is the most common slip at this step. The number 5 gives a quotient of 0 and a remainder of 5, so its pair is 05, not 5, and the code needs all 6 digits. Check by reading it back: split your HEX code into pairs and convert each pair to a decimal, as in our HEX to RGB conversion guide, and you should get your starting numbers.
Three pair boxes join into a full HEX code beside the swatch. Remember the leading zero: 5 is 05, not 5.
Worked examples
Three worked examples show the full method: rgb(255, 87, 51), rgb(52, 152, 219), and rgb(59, 130, 246). Each one divides each number by 16, turns remainders of 10 to 15 into letters, and ends as a HEX code you can check against the swatch.
Work through each example on paper before you read its answer, and write the division for every number. The first example is a warm orange-red, the second is a calm blue, and the third is the bright blue used across this site. If one pair is wrong, only that channel is wrong, so you can fix one division without redoing the whole code. The practice exercises in H2-6 give you 4 more colors to try on your own.
Worked example 1 — rgb(255, 87, 51) = #FF5733
rgb(255, 87, 51) is #FF5733. 255 ÷ 16 = 15 remainder 15, so FF. 87 ÷ 16 = 5 remainder 7, so 57. 51 ÷ 16 = 3 remainder 3, so 33. Join the pairs in order after a hash: #FF5733.
Worked example 2 — rgb(52, 152, 219) = #3498DB
rgb(52, 152, 219) is #3498DB. 52 ÷ 16 = 3 remainder 4, so 34. 152 ÷ 16 = 9 remainder 8, so 98. 219 ÷ 16 = 13 remainder 11, so 13 is D and 11 is B, giving DB. Join the pairs in order after a hash: #3498DB.
Worked example 3 — rgb(59, 130, 246) = #3B82F6
rgb(59, 130, 246) is #3B82F6. 59 ÷ 16 = 3 remainder 11, so 3B. 130 ÷ 16 = 8 remainder 2, so 82. 246 ÷ 16 = 15 remainder 6, so F6. Join the pairs in order after a hash: #3B82F6.
#FF5733
Tap a card to reveal one division at a time.
Practice exercises with answers
Try 4 colors on your own, then check your answers below. The colors are rgb(255, 0, 0), rgb(0, 255, 0), rgb(59, 91, 219), and rgb(0, 0, 0). Each one uses the same divide-by-16 method, and each answer is given in full so you can find which division went wrong.
Do each exercise on paper before you look at its answer, and write the quotient and remainder for every number. The first 2 are pure colors that test whether your pairs are in the right order. The third is the strong blue from our HEX to RGB conversion guide, so you can cross-check it in both directions. If an answer is wrong, recheck the letter values first, because remainders of 10 to 15 are the most common slip.
Practice answers
- rgb(255, 0, 0) = #FF0000. 255 is FF, 0 is 00, and 0 is 00. Pure red.
- rgb(0, 255, 0) = #00FF00. 0 is 00, 255 is FF, and 0 is 00. Pure green, also called lime.
- rgb(59, 91, 219) = #3B5BDB. 59 ÷ 16 = 3 remainder 11, so 3B. 91 ÷ 16 = 5 remainder 11, so 5B. 219 ÷ 16 = 13 remainder 11, so DB.
- rgb(0, 0, 0) = #000000. 0 is 00 for all 3 channels. Black.
Try one: what is rgb(59, 91, 219) in HEX?
Type a HEX code. The swatch stays hidden until you answer.
Reveal the full division working
59 ÷ 16 = 3 remainder 11, so 3B. 91 ÷ 16 = 5 remainder 11, so 5B. 219 ÷ 16 = 13 remainder 11, so DB. Join them: #3B5BDB.