Hexadecimal Notation the Easy Way on Your Raspberry Pi
Every programmer and hardware designer uses hexadecimal notation for one simple reason: It's easy, far easier than the alternative. The only snag is that there is a very small learning curve, so small, in fact, that you can think of it as a learning bump.
When children are taught to multiply, say three times four, to get them started, they're often told to write down a group of four marks. Then they're told to repeat that group three times and finally add up all the marks on the paper to get the answer. That is supposed to get them the basic idea, but as a long-term strategy, it's a dead end.
What a computer does for hex
Inside every computer, information is stored as a collection of binary bits: that is, a collection that you can represent by writing down a list of zeros and ones to represent each state. Inside the computer is not a sequence of zeros and ones. It's actually a system of switches that can be open or closed.
To make life easier, when designing a computer, these individual bits are arranged into groups of eight and called bytes. So each byte consists of eight bits, but what do they mean? Well, they can mean absolutely anything you want them to mean.
Depending on the context in which you find them, you can interpret the bytes as numbers, text, computer instructions, or patterns of lights to turn on. To the computer, they are all the same, just a collection of bits. That is an important thing to remember. All a computer does is manipulate bits: People put a construct or meaning on those bits.
How humans see things
Whatever those bits represent, you have to have some way of representing them, and the simplest is by writing down the string of zeros and ones. A specific byte might be represented by say 01100011. That's a bit of a difficult thing to cope with. For a start, it's a long sequence and there is very little encoding going on.
That is, each place in that representation can only be one of two things. That is great if you are a computer, but it's not how the human mind works. People work best when there is some chunking going on: that is, when the sequence is expressed with a shorter length but more variability of what can be in each place.
The obvious solution is to turn that sequence into a decimal number by having each bit represent a power of two.
Starting on the right side, you have the first bit or bit zero represent two to the power of zero or one. So if the right-most bit is a one, that contributes a one to the whole number. You can then move to the left, and the second bit represents two to the power of one, or two, and so on down the list, ending up with the left-most bit representing two to the power of seven or 128.
Then you add up all the bits in that byte that have a one in them according to their place value to get the final number of 99. This is a decimal representation of that bit pattern. So do you have it cracked? No, it requires quite a bit of mental gymnastics.
The reverse operation, getting from the decimal representation to a bit pattern, is definitely a pencil-and-paper job, but you are halfway there to making it easy.
Make hex easy for us humans
Consider breaking the byte into two — that is, two groups of four bits. A group of four bits is rather amusingly called a nibble, not quite a byte. In binary, this already looks much simpler. You would write 0110 0011.
For most people, that space in the middle makes it so much easier to keep your place. Now when you take each of those two groups and apply the same decimal conversion to that as you did to the previous byte, you get the number 63.
That's easy to remember and you can do it quite simply in your head. There are only four add-ups to do, and they are only 8 + 4 + 2 + 1 at the worst. In fact, the first group was 4 + 2 = 6 and the second 2 + 1 = 3. Great, that was easy!
But hang on, what happens when you have a result that is more than 9 in a group? Suppose you had 0000 1010. That would give you zero for the first group and 10 for the second. The number you would have would be 010.
You wouldn't know if this was a three-nibble number, 0000 0001 0000, or a two-nibble number, 0000 1010. To get around this problem, you don't use numbers to represent values greater than nine. Instead, you use letters.
So if you get to a value of ten when you add up your nibble, you call it A or in fact a. If you get to eleven, you call it B and so on up the alphabet until you get to fifteen, which is F. In that way, you can stick to one position in your number representing four bits or a nibble. You have shortened the sequences length and chunked up to a good degree.
The only problem is that you have to learn six new numbers and also learn how to indicate that you have a hexadecimal number and not a decimal one. The most widely used convention is to prefix a hex number with zero (0) x, as in 0x21. This shows it is a hex number representing the bit pattern 0010 0001. Some systems use an ampersand (&) as a prefix instead.
Learn six new numbers to master hex
Don’t try and learn all the new symbols at once: Take them one at a time. The F is easy. It's all ones. The A is just a repeat 1010, and the C is the top two bits set at 1100. Just learn those three to begin with. Then the others are easy.
The B is just one more than the A (1010 + 1 = 1011) and the D is one more than the C (1100 + 1 = 1101). Finally, the E is one less than the F (1111 – 1 = 1110).