Contents

  1. ./ascii.py
  2. ./ascii_table.py
  3. ./binaryToDecimalIterateOverCharacters.py
  4. ./binaryToDecimalIterateOverExponents.py
  5. ./decimalToBinary.py

./ascii.py 1/5

[
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/5

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

print("This program prints out part of the ASCII Table")
print("The ASCII value is followed by the character.")

for i in range(33, 127):
    print(i, "-->", chr(i))
    

./binaryToDecimalIterateOverCharacters.py 3/5

[
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

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)
    test.testEqual(binaryToDecimal("10110"), 22)
    
def binaryToDecimal(binary):
    """
    Pre: binary should be a string containing only 0s and 1s
    Post: converts binary into a decimal value and returns that value
    """
    decVal = 0
    exponent = len(binary)
    
    for char in binary:
        bit = int(char)
        exponent -= 1
        decVal += bit * 2**exponent
        
    return decVal
    
#testBinaryToDecimal()
main()

./binaryToDecimalIterateOverExponents.py 4/5

[
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)
    test.testEqual(binaryToDecimal("10110"), 22)

    
def binaryToDecimal(binary):
    """
    Pre: binary should be a string containing only 0s and 1s
    Post: converts binary into a decimal value and returns 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()
#main()

./decimalToBinary.py 5/5

[
top][prev][next]
# Converts a decimal number into binary
# By CSCI111

import test

def main():
    print("This program converts a decimal number into a binary number.")
    decNum = int(input("Enter a decimal number: "))
    print(decNum, "has a binary value of", decimalToBinary(decNum))


def testDecimalToBinary():
    """
    tests the binaryToDecimal function
    """
    test.testEqual( decimalToBinary(0), "0")
    test.testEqual( decimalToBinary(1), "1")
    test.testEqual( decimalToBinary(2), "10")
    test.testEqual( decimalToBinary(22), "10110")

    
def decimalToBinary(decimal):
    """
    Converts the decimal number (a non-negative integer) into 
    a binary number (as a string)
    """
    
    if decimal == 0:
        return "0"
    
    # Initialize the result to the empty string
    result = ""

    # Repeat until the decimal is 0:
    while decimal != 0:
        result = str(decimal % 2) + result
        
        decimal = decimal // 2

    #Return the result
    return result

    
        

testDecimalToBinary()
#main()

Generated by GNU Enscript 1.6.5.90.