Contents

  1. ascii.py
  2. ascii_table.py
  3. binaryToDecimalIterateOverCharacters.py
  4. binaryToDecimalIterateOverExponents.py
  5. binaryToDecimal.py
  6. test.py

ascii.py 1/6

[
top][prev][next]
# Conversion of a text message into ASCII
# by Sara Sprenkle

print()
print("This program converts a textual message into a sequence")
print("of numbers representing the ASCII encoding of the message.")
print()

message = input("Enter the message to encode: ")

print()
print("Here are the ASCII codes for '" + message + "':")

for ch in message:
    print(ord(ch), end=" ")

print()

ascii_table.py 2/6

[
top][prev][next]
# Create a table of numbers (ASCII) and their character equivalent
# by Sara Sprenkle

print("This program prints out the ASCII Table")

print("DEC CHAR")
print("-"*3, "-"*4)

for i in range(33, 127):
    print("%3d %4s" % (i, chr(i)))
    

binaryToDecimalIterateOverCharacters.py 3/6

[
top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the characters in the string,
# i.e., from left to right in the string.
# By CSCI111

print("This program converts a binary number into a decimal number.")

# todo: get input from the user
binNum = "10110"
decVal = 0
exponent = len(binNum)

for char in binNum:
    bit = int(char)
    exponent -= 1
    decVal += bit * 2**exponent

print(binNum, "has a decimal value of", decVal)

binaryToDecimalIterateOverExponents.py 4/6

[
top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the exponents,
# i.e., from right to left over the binary number.
# By CSCI111

import test

def main():
    print("This program converts a binary number into a decimal number.")
    
    binaryNum = input("What is your binary number? ")
    decVal = binaryToDecimal(binaryNum)
    print("The decimal value of", binaryNum, "is", decVal)


def testBinaryToDecimal():
    test.testEqual(binaryToDecimal("0"), 0)
    test.testEqual(binaryToDecimal("1"), 1)
    test.testEqual(binaryToDecimal("1000"), 8)
    test.testEqual(binaryToDecimal("1111"), 15)
    
def binaryToDecimal(binary):
    """
    Pre: binary should be a string containing only 0s and 1s
    Post: convert binary into a decimal value and return that value
    """
    decVal = 0
    for exponent in range(len(binary)):
        index = len(binary) - (exponent + 1)
        char = binary[index]
        if char == "1":
            decVal += 2**exponent
        # Alternative: 
        # bit = int(char)
        # decVal += bit * 2 ** exponent
    
    return decVal
    
testBinaryToDecimal()

binaryToDecimal.py 5/6

[
top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the exponents (i.e., from right to left).
# By CSCI111

import test

def main():
    print("This program converts a binary number into a decimal number.")
    
    binaryNum = input("What is your binary number? ")
    decVal = binaryToDecimal(binaryNum)
    print("The decimal value of", binaryNum, "is", decVal)


def testBinaryToDecimal():
    test.testEqual(binaryToDecimal("0"), 0)
    test.testEqual(binaryToDecimal("1"), 1)
    test.testEqual(binaryToDecimal("1000"), 8)
    test.testEqual(binaryToDecimal("1111"), 15)
    
def binaryToDecimal(binary):
    """
    Pre: binary should be a string containing only 0s and 1s
    Post: convert binary into a decimal value and return that value
    """
    # The starting exponent will be the length of the string-1
    exponent = len(binary) - 1
    # Initialize the result to zero
    decimalNumber = 0
    # For each bit in the binary number
    for char in binary:
        # Multiply the bit by the appropriate power of 2
        bit = int(char)
        value = bit * 2 ** exponent
        # Add this to the result
        decimalNumber = decimalNumber + value
        # Reduce the exponent by 1
        exponent = exponent - 1
    # return the result
    return decimalNumber

testBinaryToDecimal()

test.py 6/6

[
top][prev][next]
# From How to Think Like a Computer Scientist textbook

def testEqual(actual, expected):
    if type(expected) == type(1):
        # they're integers, so check if exactly the same
        if actual == expected:
            print('Pass')
            return True
    elif type(expected) == type(1.11):
        # a float is expected, so just check if it's very close, to allow for
        # rounding errors
        if abs(actual-expected) < 0.00001:
            print('Pass')
            return True
    else:
        # check if they are equal
        if actual == expected:
            print('Pass')
            return True
    print('Test Failed: expected ' + str(expected) + ' but got ' + str(actual))
    return False


Generated by GNU Enscript 1.6.6.