Monday, 24 September 2012

Octal.

If you've ever worked with machine code or assembly, or even C or C++, you will have come across binary and hexadecimal. Lesser-used number systems are binary coded decimal and octal. I've just had a quick discussion on Facebook, in which I quickly converted a number in hex into its' binary equivalent. This is easy to do, and when you know how, the same principles apply to converting a binary number to octal, although you group together every three bits, rather than every four.

But who uses octal anyway? I was asked by someone perhaps being a little complacent and because octal isn't commonly used any more by any programmer under the age of 40 (at a guess).

Not using octal doesn't present an immediate or obvious problem as most people work with decimal unless they have to use anything else. But what if you were interfacing with a database, calling a users phone number, for instance, and you cast it incorrectly? This is exactly what I did. I'll explain more about this in a mo, but first, let's look at hex and decimal.

Hex numbers begin with 0x (zero-x) and counts 0 - 15 for each place, for instance:

// 32-bit RGB value read as AARRGGBB
#define   COLOUR 0xff071dee

and with binary, you'd use a 0b prefix, and each place is either zero or one. Decimal is how you'd use it in the real world, with no leading character, but octal simply starts with 0 (zero), and here is where the problems can begin.

For those of you old enough, you may remember that, once upon a time, all telephone numbers started with zero. My area code was 0606. Inexplicably, British Telecom (as it was known at the time) changed this for no apparent reason to 01. Now, a friend of mine vented his frustrations about this. Why? What was the point? He enquired about this quite a lot, reporting back through his fanzine 8 Bit, as it seemed to be a change just for the sake of it, and no one was providing a good explanation.

Thinking back now and with the benefit of computer science-type study since, could it be that some computer systems were being tripped up by the leading zero, especially if the rest of the number looked like octal to some server somewhere? Imagine if you had a taxi business in the Greater London area, and chose a very easy to remember number like 071 777 7111. The octal value of this is very different to the decimal one, so adding the 1 immediately after the zero could have been an attempt at a remedy from it being automatically cast to octal and therefore showing a different decimal value after the casting. Well, maybe.

Could it also be now that all phone numbers start +44 nowadays because of this known problem?, especially as most mobile numbers start 07. Well, a couple of weeks ago, I was working with a young guy at Metapps on some PHP basics. He was doing a short works experience while in the local area, and was due to travel back to London soon, so we had to squeeze a lot into a short space of time.

In the task that we were working through, we were adding data entries to a database and calling it back. The example was someone's name, address and telephone number, and I put an example for the latter field of 07777777777, and called it back using some basic PHP code (well, inline SQL to be more exact), storing the retrieved data in a variable called $phoneNumber. PHP is very loosely typed and handles variables, as far as I can tell, very safely. There's really no need for leaky memory to be crashing a server, is there? But I had my C head on, and I cast the number to type int. When I echoed out the variable, I got a very strange result.

Luckily, the problem was quickly resolved by using the +44 prefix, and therefore dropping the leading zero. Remembering that I was using PHP, I also dropped the casting to type integer. I then went on to explain to the young 'un about how computers store numbers, and how compiled code and interpreters these 'scalar types' them from an area of memory, a file or a database, and how different prefixes can affect the value, with a leading zero being the prime example stating "this number is octal".

So, who uses octal anyway? Well, the question shouldn't be who, but what technologies use octal... and PHP, along with C and C++ are definitely included there, along with, I imagine, Java. Sometimes, it's good to know these things.