Contents

Introduction

Before we begin our article we need to know first what is decimal and what is hexadecimal.

Using decimal representation means using base-10 representation of numbers which means that every digit of the number can be one of 10 values, 0 through 9.

A good example is 195. It’s is 3 digits and every digit is between 0 and 9.

On the other hand, hexadecimal is base-16 representation which means that there are 16 of base values that every digit in the number can be one of. These base values are 0 through 9 and A through F. From 0 to 9 are the same in dec (the shorthand for decimal). A equals 10, B equals 11, C equals 12, etc.

An example is 14 (decimal). If we need to represent it in hexadecimal we can say D.

Another good example is 9 (decimal). If we need to represent it in hex (the shorthand for hexadecimal,) you can say 9 too.

Color representation

You already know that a colors consists of 3 values; RGB (Red, Green and Blue). Although, there’re times when we blend it with another value (Alpha) to make color transparent, however it is not common. So we end up with 4 values (ARGB) to represent our color. True? Every value of these four occupies a single byte in memory so every value ranges from 0 to 255.

In HTML for instance you can represent any color by its name (if it is a known color like Red) or by combining the RGB values using hexadecimal (HTML doesn’t support Alpha.) You cannot use decimal values to represent colors in HTML. So you need to know how to convert between every decimal value to its equivalent hexadecimal and vice versa.

Converting decimal to hexadecimal

You already know that decimal values from 0 to 15 have the hex counterparts 0 through 9 and A through F so conversion will be very easy in this range. For a decimal value greater than 15 you need to apply a simple formula to convert it.

First, divide the value by 16 (because hex is base-16), then add the hex result (e.g. A for 10) to the left.

Second, divide the value by 16, and then add the remainder (modulus) to the right of the last number.