Magic Hexadecimal and High Octane Octal

This article is part of a series, access the series index.

Hexes and racecars don’t really go together unless you’re in Wacky Races, but if that’s the case you may be a lost cause (but can we hang out later?) In this article we’re going to tackle witchy hexadecimal and not let octal take us for a fast one.

Recommended prior reading: The Wonderful World of Binary – trust me, if you go through this article you’ll be more well informed of number bases, and computer representations. This article will refer to it.

Hexadecimal and Octal Are Just Two More Ways To Count

Remember in binary we talked about number bases such as base 10, base 2, etc? Well, hexadecimal and octal are simply two more ways to count, meaning they are two number bases. They are handy in computer programming and science for a variety of reasons as will be explained later.

Hexadecimal refers to base 16 (hex – 6 and deca – 10), while octal refers to base 8 (oct – 8, like octagon.)

Base 16, or Counting With Letters

If there’s only ten digits, 0 – 10, how do we count up to sixteen? Simple, we use letters. So hexadecimal ‘counts’ as such:

0… 1… 2… 3… 4… 5… 6… 7… 8… 9… A… B… C… D… E… F… 10…

Oh, whoah, wait, A… D… 10? This might make more sense if we do this:

0… 1… 2… 3… 4… 5… 6… 7… 8… 9…
A (10)… B (11)… C (12)… D (13)… E (14)… F (15)…
10 (16)… 11 (17)… 12 (18)… 13 (19)… … …

Do you see how we keep counting up, it’s just we use the letters A, B, C, D, E, and F as more ‘digits’? In this case, just like powers of 2 in binary, the place values are powers of 16. This makes the ‘tens place’ actually the ‘sixteens place’, and the ‘hundreds place’ actually the “256’s place”.

hex_placement

For example: the hexadecimal number A2, would be (10 * 16) + (2 * 1) = 162.

Base 8, or Counting… With Fewer Numbers

In octal, you only use the digits 0 – 7. Why? Well, octal only counts ‘up to’ 8, just as decimal counts up to 10, binary up to 2, and hexadecimal up to 16. So how do we count only using 8 digits (0 – 7). Like so:

0… 1… 2… 3… 4… 5… 6… 7… 10… 11… 12… 13…
14… 15… 16… 17… 20… 21… … …

You can see that unlike in decimal the next place value is incremented after every eight counts. So, like powers of 10 in decimal, powers of 2 in binary, and powers of 16 in hexadecimal, octal operates on powers of 8:

oct_placement

For example: the octal number 75, would be (7 * 8) + (5 * 1) = 61.

So, Why Would You Do This?

Well, so this seems rather esoteric and obscure, why bother with this heady stuff? Convenience, amazingly enough. Remember binary? Remember how I said that binary gets stored in bytes that are 8 bits (1’s or 0’s) long? Well, it just so happens 1 hexadecimal digit always maps to 4 bits, no matter what placement value it is in:

hex_demo

Isn’t that amazing? I thought it was amazing the first time I saw it. I couldn’t believe one digit or letter in one place could always ‘represent’ the binary in the same place until I really worked it out in my head one day. But there it is!

Truly this process is true of any two number bases with matching boundaries (such as base 4 and two binary digits), but as for our article, we’ll show octal next. Each octal digit in its place can (but not always in the real world) represent 3 binary digits:

oct_demo

Representing Data More Easily

Remember how we talked about measuring information? A byte is 8 bits (a 1 or 0) of binary data. So when we write out data in hexadecimal format we can reduce writing eight 1’s and 0’s down to two numbers/letters. So a byte would become something like 9E, or F1 or 12. You can see that this is handier and friendlier to display and to deal with than to look at eight or more ones and zeros.

But what about octal? Octal doesn’t fit into the 8-, 16-, 32-, etc. size data chunks like hexadecimal does. While octal is still used in some instances (we’ll get to one of them), it was more widely used in the past when systems such as the PDP-8 and IBM mainframes employed 12-, 24-, and 36- bit ‘words’. Octal fit into these constraints and on top of that could be displayed by a regular calculator-like display (using only digits).

Hexadecimal and Octal in Action

One modern use of hexadecimal is specifying the colors found on websites. This is an extension of the example of binary use in the article on binary. In the websites ‘code’, either (X)HTML, or CSS, the colors displayed (particularly text, and borders, though graphics programs often use the same system) are represented by a 6 digit hexadecimal number. Every two digits represent a primary color (which again is red, green, and blue in computer land). They are written out as such: #XXXXXX, with each X being a number from 0 – F; making XX from 00 (0) to FF (255). The higher the number, the greater the intensity of the primary color. The order of the colors goes like this: #FFFFFF. An example of a random color: #520712. White is #FFFFFF, and black is #000000.

A modern use of octal is specifying file and folder permissions in a UNIX-like environment (UNIX being a categorization of operating systems). These operating systems have three ‘levels’ of permissions: owner, group, and everyone. Each level has three ‘flags’ or bits of info: read (r), write (w), and execute (x). That gives us 9 bits of information: owner – [rwx] group – [rwx] everyone – [rwx] -> rwxrwxrwx. Each ‘flag’ or bit is either true or false, such as readable or not readable, writeable or not, and executable or not. 9 is a multiple of 3, and every 3 bits can be represented by an octal digit right? Right.

So now our permissions don’t have to look like this: 101001011 or r-x–x-wx. Oh no, they can look like this: 513.

oct_permissions

Casting Spells and Racing Cars

When you’re programming you can represent numbers (‘literals’) just by writing them (most of the time. So, there’s got to be some way you can represent a hexadecimal and octal number when you want to. Especially with octal you can’t just write a straight number because there’s nothing that makes it look different from any other decimal number. The same thing really in hexadecimal if there aren’t any letters in that particular number. So how do you do it?

There are MANY ways to represent hexadecimal numbers that I wrote an article demonstrating them all (based on Wikipedia). However, there are only a few ways octal is usually represented.

There are a few ways octal digits are normally represented in different programming languages. For any particular language, you’ll have to refer to its specification. In some languages, you just put a 0 (zero) in front of the number, or the letters o (oh) or q, and even the digit-letter combination 0o (zero-oh). There is a particular context where you can ‘escape’ an octal character. ‘Escaping’ is out of the scope of this article, but you’d do that by writing something like this: \001 (1 in octal).

Tada!

That’s all there is to hexadecimal and octal. As you can see, hopefully, it’s not too complicated and is quite a bit like binary. Hexadecimal and octal were introduced to me when I was editing data files in a text editor that displayed the hexadecimal values of each byte as well. It was a pretty useful program.

Well, that’s it for now!

If you appreciate my tutorials please help support me through my Patreon.

photo credit: 40h. via photopin (license)

Liked it? Take a second to support kadar on Patreon!

kadar

I'm just a wunk, trying to enjoy life. I am a cofounder of http//originalpursuitssoc.com/ and I like computers, code, creativity, and friends.

You may also like...

1 Response

  1. May 18, 2017

    […] system using the numbers 0-9 and the letters A-F.  For more information on hexadecimal please see my tutorial on the […]

Leave a Reply

%d bloggers like this: