Log in / Register
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

1.7 Hexadecimal and Octal Representation

Most programmers do not have to work with binary number representations. Programming languages let programmers write numbers in base 10 and they do the conversion for us. However, once in a while a programmer must be concerned about the binary representation of a number. As we've seen, converting between binary and decimal isn't hard, but it is somewhat tedious. The difficulty arises because 10 is not a power of 2. Converting between base 10 and base 2 would be a lot easier if 10 were a power of 2. When computer programmers have to work with binary numbers they don't want to have to write out all the zeroes and ones. This would obviously be tedious as well. Instead of converting numbers to base 10 or writing all numbers in binary, computer programmers have adopted two other representations for binary numbers, base 16 (called hexadecimal) and base 8 (called octal).

In hexadecimal each digit of a number can represent 16 different binary numbers. The 16 hexadecimal digits are 0–9, and A–F. Since 16 is a power of 2, there are exactly four binary digits that make up each hexadecimal digit. So, 00002 is 016 and 11112 is F16. So, the binary number 10101110 is AE in hexadecimal notation and 256 in octal notation. If we wish to convert either of these two numbers to binary format the conversion is just as easy. 10102 is A16 for instance. Again, these conversions can be done quickly because there are four binary digits in each hexadecimal digit and three binary digits in each octal digit.

Example 1.3 To convert the binary number 010100112 to hexadecimal wehave only to break the number into two four digit binary numbers 01012 and00112. 01012 = 516 and 00112 = 316. So the hexadecimal representation of010100112 is 5316.Python has built-in support of hexadecimal numbers. If you want to expressa number in hexadecimal form you preface it with a 0x to signify that it is ahexadecimal number. For instance, here is how Python responds to 0x53 beingentered into the Python shell.

Kent 's Mac > python

Python 3.1.1 (r311 :74543 , Aug 24 2009, 18:44:04)

[GCC 4.0.1 (Apple Inc. build 5493)] on darwin

Type "help", "copyright", "credits" or "license" for more info.

>>> 0x53


>>> 0o123



Since 8 = 23, each digit of an octal number represents three binary digits. The octal digits are 0–7. The number 010100112 = 1238. When converting a binary number to octal or hexadecimal we must be sure to start with the right-most bits.

Since there are only 8 bits in 01010011 the left-most octal digit corresponds to the left-most two binary digits. The other two octal digits each have three binary digits. Again, Python has built-in support for representing octal digits. Writing a number with a leading zero and the letter o means that it is in octal format. So 0o123 is the Python representation of 1238 and it is equal to 8310.

Practice 1.8 Convert the number 5810 to binary and then to hexadecimal and octal.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science