Octal and Hex

Today we will cover Octal and Hexadecimal numbering systems. We first looked at the Ascii Table below.

Hexadecimal is another place value numbering system that has 16 as its base. From previous discussions you might remember that we will need to have 16 different symbols. In this case it is usual to use the following symbols to represent this system.

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

We had to have symbols that went past 9 so we just picked some that were already on the keyboard. Note in the third column of the table above there are numbers that contain letters. For example 03E is the hex code for the greater than symbol >. Ok, so how do we think about hex numbers. The same way we do decimal number except that we are using base 16. The right most digit is the units digit, the next to the right digit is the 16′s digit, the next one is the 16 squared digit etc. These are the values of each digit in powers of 16. Well, what do the letters mean?. Its simple, they are just single symbols that represent the following 2 digit decimal numbers, A =10, B=11, C=12, D=13, E=14, F=15. During a conversion you just use 10 when you see and A etc.So, what is the meaning of the hex number 2A4? It’s base 10 value is thus

2A4(base 16) = 2*(16^2) + 10*16 +4*1=2*256+4*16+4*1=676(base 10)

If you do not already know, * represents multiply and ^ represents “to the power of”, i.e. the exponential operator. Lets check another one.

3B1D(base 16) = 3*(16^3)+11*(16^2)+1*(16)+13*1= 15133(base 10)

Look closely at the table above and check some of the conversions found there. For example, in the last line, hex 44 is shown to be the same as decimal 68. Is this true?

Binary to Hex and Back

It turns out that it is very easy to convert back and fourth between hex and binary. You just convert every four digits (from the right) to its corresponding hex digit. Let review these conversions using exactly four digits. The 0 is 0000 in binary, the 1 is 0001 in binary, the 2 is 0010 in binary, the 3 is 0011…., the 9 is 1001, A is 1010, B is 1011, C is 1100, D is 1101, E is 1110 and finally F is 1111. Note that we use all possibly four digit binary numbers to represent these 16 symbols. Capice?

So if we have a big binary number, say 101101011110101111. We start by breaking the bit string up into groups of 4 from the right end. In this case we get 10 1101 0111 1010 1111. Now it is very easy to convert each quad into its corresponding hex code. Here we get 2D7AF. Note that the leading 10 is not four digits so we just add 2 zeros to the front to get 0010 and then convert that.

Even though I expect you to be able to do this by hand I will point out a simple tool that will allow you to convert easily. This tool is the standard calculator that comes with Windows XP. Click START then click All Programs and select Accessories and then calculator. There are two views of this calculator, Scientific and Standard. Select the Scientific version and you should get the following.

This is Calculator in scientific mode for XP. If you want the standard mode just click View and It will allow you to switch.Note that the Hex circle is clicked so that the user can type in a hex value. I have typed in 2D7AF. How did I type in letters? Look down at the bottom. Here you will see the letters A thru F. Click these when you need them or just use the keyboard.When you have entered the number then all you need to do is change the display type by clicking on the Bin circle. The displayed value will not be automatically converted to binary. It should show give the following 101101011110101111


Base 8 or as it is usually known as Octal is another place value system. Being base 8 it only uses the symbols {0, 1, 2, 3, 4, 5, 6, 7}. The value of each place in a number is now powers of 8. So the right most digit is the units digit, the next is the 8′s digit, then next is the 8 squared digit etc. The octal number 12307 can be converted to base 10 as follows.

12307( base 8 ) = 1*(8^4)+ 2*(8^3)+ 3*(8^2)+ 0*(8^1)+ 7*(8^0)=5319 (base 10)

= 1*(4096)+ 2*(512)+ 3*(64)+ 0*(8)+ 7*(1)

=5319 (base 10)

Recall that 8^0 is a number to the zero power and all non-zero numbers to a zero power is 1. Check the above by hand and also using the calculator. The calculator also has Oct as one of its options. You can use it to convert between any of the four formats, Hex, Dec, Oct and Bin. Its very handy in this case.

Convert by hand from binary to octal is also very easy. It is just like the binary to hex method discussed above, except that now we break the long binary number up into groups of three. For example the number 101101011110101111 is grouped as below

101 101 011 110 101 111

Convert each triple we get 553657. A quick use of the calculator bears this out.

NOTE 1: ASCII code has been around for some time and we normally represent it using either binary or hex, mainly since we really use these as representative coding. A byte is repsented by either 8 binary digits or 2 hex characters. The numerical representations are NOT meant to be used in calculations. I.E. you do not normally add two ASCII codes for the same reason you would not add two social security numbers.

NOTE 2: The ASCII table has two problems. One it is American and two it can only represent 256 different things. WHY? To deal with these problems there is another coding method call Unicode. This is a international standard that can use either 8, 16 or 32 bits. These additional bits are used to represent many things including the symbols used by the many languanges in the world, ie Chinese, Arabic etc. The above link is to the Unicode home page.

Comments are closed.