10/13/2020

# Decimal Numbers

Our ordinary numbers use 10 as a base. To see what this means, consider the number 367. Written out in a way that clarifies the role of 10, we have $367 = 3*10^2+6*10^1+7*10^0$

# The Octopus View

Suppose we thought like octopi and always based our thinking and our numbers on 8. If an octopus were to write 367, what would it mean in the eyes of a 10-fingered creature?

The difference is that the octopus uses 8 as a base instead of 10.

$3 * 8^2 + 6 * 8^1 + 7*8^0$

What is the actual decimal value of what the octopus wrote as 367. You can do this in your head or use python.

3 * 8**2 + 6 * 8**1 + 7 * 8**0
## 247

How many conversations do you have on a daily basis with octopi?

How many conversations do you have on a daily basis with computers?

Fortunately, computers (or the software running on them) are designed to communicate with us in ways we understand.

However, some times we need to look more closely at what a computer is doing. To a computer, the natural number base is 2.

If a computer said “1011” to you, what would it mean?

$1 * 2^3+0*2^2+1*2^1+1*2^0$ We would describe this number as 11 in our base 10 system.

Note than in all three systems, there is no single character to give the value of the base. The base is written as 10 in all of them.

This means ten in the decimal system, eight in the octal system and 2 in the binary system.

Communicating directly with a computer using pure binary numbers would be very awkward. Normally we use an intermediate format, hexadecimal, base 16, numbers.

Hexadecimal numbers require single symbols to represent every decimal value between 0 and 15. The symbols 0 through 9 are augment with the letters A through F. A is the hexadecimal digit with the decimal value of 10. F is the hexadecimal digit with the decimal value of 15.

Instead of a binary number 11111111, we can write the hexidecimal number FF. Both have the decimal value 255.

# Conversions in Python

Go to https://docs.python.org/3/library/functions.html and investigate int(), bin(), and hex(). Then use these functions to convert some examples.

print(int("11111111",base = 2))
## 255
print(int("FF",base = 16))
## 255

We can go the other way using bin() and hex()

print(bin(255))
## 0b11111111

Note the prefix 0b to indicate a binary representation.

print(hex(255))
## 0xff

Note the prefix 0x to indicate a hexadecimal representation.

print(0xff)
## 255

Note that the value displayed by print() is the standard decimal representation.

It is important to keep in mind that there is an underlying value, which can be represented in different ways.

To reinforce all that I’ve said so far, I suggest you look at Corey Schafer’s video on the same topics at https://coreyms.com/development/understanding-binary-hexadecimal-decimal-base-10

# Why???

Why do we need to be familiar with hexadecimal numbers?

We aren’t going to use them for computation.

We will see them when we inspect the exact contents of a file or computer memory.

# ASCII

The American Standard Code for Information Interchange is our starting point. Look at the table at http://www.asciitable.com/.

The ASCII characters are numbered from 0 to 127. Note that there are 128 ($$2^7$$) values. To represent one of these characters in human/digital form, use the hexadecimal equivalent of the character number.

As an example character number 32 is a blank space, which has the hexadecimal representation 20. The hex 20 is a binary 0010 0000 on a digital device. I added the space for readability. Note that there are 8 bits since the standard unit of computer memory is 8 bits, one byte.

There are python utility functions to help make this clear.

# ord()

The function ord() accepts a character and returns the character number (decimal).

Exercise:

Use ord() to find the character numbers for the blank space, the number 7, and the characters “A” and “a”.

print(ord(" "))
## 32
print(ord("7"))
## 55
print(ord("a"))
## 97
print(ord("A"))
## 65

# chr()

The chr() function accepts a character number and returns the character.

Exercise:

Use the chr() function to display the characters with numbers 41, 62, 83, and 120.

print(chr(41))
## )
print(chr(62))
## >
print(chr(83))
## S
print(chr(120))
## x

# A Table

Iterate over the string “Hello, world!”. Print out the character, the number of the character, and the hexadecimal representation.

s = "Hello, world!"

for i in s:
print(i, ord(i), hex(ord(i)))
## H 72 0x48
## e 101 0x65
## l 108 0x6c
## l 108 0x6c
## o 111 0x6f
## , 44 0x2c
##   32 0x20
## w 119 0x77
## o 111 0x6f
## r 114 0x72
## l 108 0x6c
## d 100 0x64
## ! 33 0x21

Note again that python indicates hexadecimal numbers using the prefix “0x”.