What Is Hexadecimal and Why Do Programmers Use It?

If you have ever seen a color like #1ED678 or a memory address like 0x7FFE and wondered what is hexadecimal and why do programmers use it, this guide answers both. Hexadecimal is a way of writing numbers with 16 symbols instead of 10. Programmers use it because it is a short, readable way to write the binary that computers actually store. Below you get the rules, two conversion methods you can do by hand, and real values pulled from the code of a tank game.

What is hexadecimal and why do programmers use it? A base 16 number system explained

Hexadecimal, or "hex", is a base 16 number system. The decimal system you use every day is base 10: each place is worth ten times the place to its right. Hex works the same way, but each place is worth sixteen times the place to its right.

Computers store everything as bits, which are 0 or 1. Long strings of bits are hard for people to read and easy to mistype. Hex gives you a compact shorthand. One hex digit stands for exactly four bits, so two hex digits describe one full byte. That clean fit is the whole reason hex exists in programming.

How hex digits work: 0 to 9 and A to F

Base 16 needs 16 symbols. We have only ten digits, so hex borrows six letters:

  • 0 to 9 mean zero to nine, as usual.
  • A means 10, B means 11, C means 12.
  • D means 13, E means 14, F means 15.

After F, you run out of single symbols and carry over, just as decimal does after 9. So 10 in hex is not ten. It means one sixteen and zero ones, which is sixteen in decimal. Letters can be upper or lower case: ff and FF are the same number.

You will often see a prefix that tells you the number is hex. 0x is common in code (0xFF), and # is common for web colors (#FF0000). The prefix is a label, not part of the value.

Hexadecimal vs binary vs decimal: a side-by-side comparison

Here is the same set of numbers written three ways. Read each line as decimal, then binary, then hex.

  • 16: decimal 16, binary 10000, hex 10
  • 30: decimal 30, binary 11110, hex 1E
  • 120: decimal 120, binary 1111000, hex 78
  • 214: decimal 214, binary 11010110, hex D6
  • 255: decimal 255, binary 11111111, hex FF

Notice the pattern. The biggest value that fits in one byte, 255, takes eight binary digits, three decimal digits, and only two hex digits. Hex is the shortest of the three, and unlike decimal it lines up with the bits underneath.

Diagram splitting the byte 11010110 into 1101 and 0110, which are the hex digits D and 6

One byte is two hex digits: 1101 is D and 0110 is 6.

Why programmers use hexadecimal instead of binary

Binary is what the hardware uses, so why not write binary? Because people make mistakes with long runs of ones and zeros. Is 110101101111 the same as 110101001111? You would have to look hard.

Hex fixes that without hiding the bits. Split the binary into groups of four, and turn each group into one hex digit:

  • 1101 is D
  • 0110 is 6
  • 1111 is F

So 110101101111 is D6F. You can go back the other way just as easily. Decimal cannot do this, because 10 is not a power of 2. Converting decimal 214 to bits means doing arithmetic. Converting hex D6 to bits means looking up two small groups.

That is the short answer to why programmers use hexadecimal: it is easy to read, easy to type, and maps straight onto bytes.

How to convert hex to decimal and decimal to hex step by step

Hex to decimal

Multiply each digit by its place value, then add. The rightmost place is 1, the next is 16, the next is 256.

Take 2F:

  1. The 2 is in the sixteens place: 2 x 16 = 32.
  2. The F is in the ones place: F is 15, so 15 x 1 = 15.
  3. Add them: 32 + 15 = 47.

So 2F is 47. Now a longer one, 4E20. The places are 4096, 256, 16 and 1. That gives 4 x 4096 = 16384, E (14) x 256 = 3584, 2 x 16 = 32, and 0. The total is 16384 + 3584 + 32 = 20000.

Decimal to hex

Divide by 16 again and again. The remainders, read from last to first, are your hex digits.

Take 200:

  1. 200 divided by 16 is 12 remainder 8.
  2. 12 divided by 16 is 0 remainder 12, which is C.
  3. Read the remainders from last to first: C, then 8.

So 200 is C8. Check it: 12 x 16 + 8 = 200.

Want to make this stick? Do not just reread the steps. Cover the answer and convert five numbers from memory. It helps to know why the sizes line up: a bit is one 0 or 1, and powers of two explain why 4, 8 and 256 keep showing up.

Where you will see hex in real code: colors, memory addresses, and bytes

Once you know the rules, hex shows up everywhere.

  • Colors. A web color like #FF8800 is three bytes: red, green, and blue. Each pair of hex digits is one color channel from 0 to 255. FF is full red, 88 is half green, 00 is no blue.
  • Memory addresses. Debuggers show addresses in hex because an address is a byte position, and bytes are two hex digits each.
  • Raw bytes. File viewers, network tools, and error dumps print data as pairs like 3F A0 07.
  • Flags. A single byte can hold eight on/off switches. Hex lets you say which are on, such as 0x05 for the first and third.

How game developers use hex values for colors, IDs, and flags in a tank game

Games are full of small numbers, so they are a good place to see hex in use. Tank City Reboot is a free browser tank game set inside a computer, and its code has plenty of values you can read this way. The code stores them as plain numbers. Here is what they look like when you write them in hex.

Colors

The game draws every texture in code. Each pixel is four bytes: red, green, blue and alpha (how solid it is). The light green pixels of an encrypted zone (the code calls it fog) are red 30, green 214, blue 120, at alpha 235. Convert each channel:

  • 30 is 1E
  • 214 is D6
  • 120 is 78
  • alpha 235 is EB
A green swatch beside the channels 1E, D6 and 78 that make the web colour #1ED678

The encrypted zone green, channel by channel.

As a web color, that is #1ED678. The shield ring around your tank is red 150, green 245, blue 255 at alpha 230. That is #96F5FF with alpha E6. Notice the FF: blue is at its maximum.

Sizes and scores

The first game's board is 208 by 208 pixels. In hex, 208 is D0. A tank is a 16 by 16 box, and 16 is 10, a nice round number in hex. The game also gives you an extra life the first time your score reaches 20,000, which is 4E20, the number we converted above.

IDs and flags

The game stores its map in tiny squares. Each 4 by 4 pixel square is one byte, and that byte is one value from a list: empty, brick, steel, water, fog (the encrypted zone), ice, base. Those names are numbered from 0 upward. One byte can hold up to 256 values, written 00 to FF, so a list of seven kinds fits with room to spare. This is the same idea as a flag byte: a small number in one byte stands for a state.

Time is a number too. Every action has one, such as a dash that takes 1.5 seconds to recharge before any Overclock upgrade. To see how, read cooldowns in video games: how a tank game times every action. Zero Day, the second game at tankcityreboot.vlvd.net/zero-day, uses the same kind of numbered lists for its enemies and its ten upgrades.

Try it yourself. Pick any number you see in a game, convert it to hex, then convert it back. If you land on the same number, you got it right.

Frequently asked questions

What does hexadecimal mean?

Hexadecimal means base 16. It counts with 16 symbols, 0 to 9 and A to F, instead of the ten digits in decimal.

Why do programmers use hexadecimal instead of binary?

Binary strings are long and easy to misread. One hex digit equals exactly four bits, so hex is a short, exact shorthand for binary.

What does the 0x prefix mean?

It tells the reader or the compiler that the number that follows is in hex. 0x10 is sixteen, not ten.

What is FF in decimal?

FF is 255. That is 15 x 16 + 15, and it is the largest value one byte can hold.

Why do hex colors have six digits?

Each of the three color channels, red, green and blue, is one byte, and one byte is two hex digits. Three channels times two digits gives six.

Get started with Tank City Reboot

The best way to get comfortable with hex is to meet it in code you can poke at. Pick a value, convert it, check it, and repeat. If you want a break from the math, play a round of a game whose numbers you now know how to read.

Play Tank City Reboot: it is free, plays in your browser on a computer or a phone, and needs no account.

0 likes

Comments

No comments yet.

Sign in or make an account to comment.