What is Binary and Hex? How to Convert to any Number Base - The Ultimate Cheat Sheet

What is Binary and Hex? How to Convert to any Number Base - The Ultimate Cheat Sheet

A full insight into binary, hexadecimal , decimal and other number bases. You are going to easily learn how to work in any number base and convert between any number base.

This is very close to the lesson our Dad gave each of us without the hard bits.

It's really important for coding and hacking to understand number bases and you will be able to decode the secret message below. You'll learn tricks too to quickly convert hex and binary, and learn about bits, bytes, and nibbles.

┌──────┬───┬───┬───┬───┬───┬───┬───┬───┐
 Hex   -  -  -  -  -  -  -  - 
├──────┼───┼───┼───┼───┼───┼───┼───┼───┤
 0x00                         
 0x66                         
 0xFF                         
 0xFF                         
 0x7E                         
 0x3C                         
 0x18                         
└──────┴───┴───┴───┴───┴───┴───┴───┴───┘

Sit back and watch the video below and don't forget to read on for extra explanations, information and resources. The video introduces the grid system we use, which may seem odd at first. Stick with it, remember we are giving you the tools to work in any number base which takes a little extra time to explain.

We have a really great HEX sprite creator tool click here to check it out.

This is the basis to work in any number base, to simplify we deal with only integers to start. Just like regular decimal (base 10) we all use every day we do the following.

  • Every column represents a number e.g. 100s,10s,1s
  • There is a digit in each column
  • Multipy each digit by the value it's column represents
  • Add everything up
b2 (10x10) b1 (10) b0 (1)
1 2 3

So for 123 in base 10 we have (1x100) + (2x20) + (3x1) = 123

Base b3 b2 b1 b0
Dec (b=10) 103 (1000) 102 (100) 101 (10) 1
Bin (b=2) 23 (8) 22 (4) 21 (2) 1
Hex (b=16) 163 (4096) 162 (256) 161 (16) 1

The 1st column always represents 1s. The 2nd column represents the same number as the number base you are working with. So for base 10 the 2nd column represents 10s, in base 2 2s, and base 16 16s.

Each column is B times larger than the column to it's right (where B is the number base). So in binary each column is twice the value of the column to the right, in base 10 they get 10 times the value etc.

Another way to think about it is if you call the 1st column column 0, the next column 1 etc. then the column represents the base you are in raised to the power of the column.

Let's do a couple of examples, 3rd column (column 2 is we start at column 0) base 16 would be 16 to the power of 2 or 16x16 with is 256.

4th column (column 3 if we start at column 0) base 2 would be 2 to the power of 3 or 2x2x2 which is 8.

Most people will know binary is only 0s and 1s, you can't use the digit 5 in binary. So how do we decide how many digits?

The digits you can use range from 0 to 1 less than the number base you are in. So base 10 you can have 0 to 9 and in base 3 you can have 0,1 and 2.

All very well so far, but Hex base 16 is used and we run out of digits at 9. This is because we use base 10 more often than not.

You can pick any digit but in computer science we move on to letters. So 10 would be A, 11 would be B and 15 would be F.

Can we have fractional parts like we have in day to day decimal like 1.5? We sure can, we use the same . sperator and start adding columns to the right.

The first column to the right of the . will be 1 divided by the number base. So in decimal we know we have 1 divided by 10 or 10ths. You can keep going forever each additional column to the right gets divided again by the base you are in.

In binary we have 1 divided by 2 or halves. So 1 and a half in binary would be 1.1

In hex the 1st column to the right of the . seperator would be 16ths so 1 and a half in base 16 or HEX would be 1.8 (one and 8 16ths)

We all know how easy it is to multiply and divide by 10 in decimal, in binary we get the same thing with multiplying or dividing by 2. Try it!

There is a special relationship between binary and HEX which is why Hex is used to often in coding and hacking.

You may know a bit is one binary digit, and a byte is 8 bits. What is less common to hear is nibble, which is 4 bits.

The trick is that each binary nibble is a hex digit which makes converting between the two trivial. Let's take and example.

Convery Binary number 10110110 to Hex.

Seperate into nibbles [1011] and [0110]

1011 is (1x8)+(0x4)+(1x2)+(1x1) = 11 which is B in Hex

0110 is (0x8)+(1x4)+(1x2)+(0x1) = 6 which is 6 in Hex

So 10110110 is B6 in Hex.

Let's try a big number EE0021 which is 6 nibbles or 3 bytes so 16,777,216 possible values but using our nibble trick the maths is easy.

Convert E to a binary nibble - E is 14 in decimal 1110 in binary

0 is just 0000, 2 is 0010 and 1 is 0001 so ...

EE0021 is 111011100000000000100001

You will often see 0x before a hex number and 0b before a binary number when you see it in code.

0xEE0021
0b10110110