Tell you what, here's a writeup on

hexadecimal (or

*hex*) that's actually

**useful**.

### Everything you always wanted to know about hex but..

No, scratch that, it's far too bad a pun. M'kay, try again..### FryingLizard's Useful Guide To Hex (and Binary, along the way)

As you know, hex is used by geeks to specify numbers. It's useful because it is (a) compact to write/type, and (b) relates closely to

binary, which is what computers are all about.

**How it works**

Numbers that are in hex are usually indicated by either a '$' prefix (general use), a '0x' prefix (common - used in C and other programming languages), a '&' prefix (archaic, used on some 8-bit computers), or, occasionally, the letter 'H' on the end. So, you could see hex numbers written as $1234, 0x1234, &1234, or 1234H.

A hex digit can go from 0 to 15, and as the familiar decimal system runs out at '9', we have to cheat and start using letters to represent values from 10 to 15, so we use A-F.

So, the hex number $F is '15' in decimal, $9 is just plain '9' in decimal, and $10 is '16' in decimal...

Why? Well, obviously in decimal the number '10' means 'one times 10 plus zero'. Because hex goes up to 15, '$10' means 'one times 16 plus zero'. Hence, $18 is 'one times 16 plus eight', or 24.

If you follow on from this, $100 is 'one times **16 times 16**, plus zero times 16, plus zero', or 16*16, which = 256.

It can be helpful to memorise (well, it just kinda sinks in after a while) the following;

$10=16, $100=256, $1000=4096 (16*16*16), $10000=65536, and so on.

So, $FFFF (in at the deep end) is actually 6553**5**, because it's one less than $10000, or of course you could work it out the long way, as ($F=15, so 15*4096 + 15*256 + 15*16 + 15 = 65535)

**Why do we bother? Are we just hopeless geeks?**

Well, because, like I said earlier, hex is actually fairly closely related to binary.

In binary the only numbers are 1 and 0, and this is all computers *really*, deep down, understand - this is because all the little wires inside your computer can only be in two possible states - 'voltage going down the wire' or 'no voltage going down the wire', i.e. 1 or 0.

Binary numbers are usually notated by a '%' prefix, so %1 is just 1, %10 is 2 (1 times 2, plus 0), %1000 is 8 (1 times 2*2*2), and so on. Thing is, binary numbers are a pain in the arse to deal with, because they get pretty long, for example 12345678 is %101111000110000101001110, which clearly sucks fairly badly.

M'kay, so binary is too cumbersome, why is hex better?

Well, because hex - also known as base 16 (because the numbers go from 0 to 15; 16 possible values) - is based on a power of two (2*2*2*2 or 2^{4}),

and binary *is* powers of two, so in fact a single hex digit directly corresponds to four binary digits. (by the way, **bi**nary digi**ts** = bits)

The number 10 in decimal is $A in hex, and %1010 (1 times 2*2*2 plus 1 times 2 = 8 + 2 = 10) in binary, $F in hex = %1111, so $F0 in hex is %11110000. M'kay?

If you treat each hex digit as directly being equal to 4 bits (4 bits is called a nibble, by the way, same as 8 bits is called a byte) you can see that hex is just a nice compact way of handling the binary numbers that make the computer run.

For if a computer uses 8 physical wires together (called a bus) to represent a number, you have 8 bits, so you can represent a number from %00000000 (0) to %11111111 (255) over those 8 wires. As you can see '255' isn't exactly an intuitive number because we're using decimal, but %11111111 is $FF in hex, which fits far more nicely.

It gets far worse when the numbers get larger; %1111111100000000 is '65280' in decimal, but a nice clean $FF00 in hex.

If you want to convert %1010000010100000 into decimal it's a pain in the arse (it's 41120, by the way), but converting it to hex is relatively easy - you just break the binary number into sets of 4 bits and away you go - remember that %1010 is 'A', so you get $A0A0.

By the way, although geeks like me are fairly good at converting between hex and decimal in our heads (you just get used to it after a while), most sane people use a calculator (e.g. the Windows calculator in 'scientific' mode will do it).

Hope this helps!

Love,

**FryingLizard**