Contents

  1. ascii.py
  2. ascii_table.py
  3. binaryToDecimalIterateOverCharacters.py
  4. binaryToDecimalIterateOverExponents.py
  5. binaryToDecimal.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.
# Switch to the commented-out version after we know how to do string
# formatting.  The table will look prettier.
# by Sara Sprenkle

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

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

for i in range(33, 127):
    print(i, "-->", chr(i))
    # prettier format; we'll know how to do this on Friday:
    # print("{:3d} {:>4s}".format(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

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

[
top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the exponents.
# Exercise: implement two ways:
#   --> iterate over the binary string from the last character to the first
#   --> iterate over the binary string from the first character to the last
# By CSCI111

import test

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


def testBinaryToDecimal():
    """
    tests the binaryToDecimal function
    """
    test.testEqual( binaryToDecimal("0"), 0)
    test.testEqual( binaryToDecimal("1101"), 13)
    test.testEqual( binaryToDecimal("10110"), 22)
    
    
def binaryToDecimal(binaryNum):
    """
    Precondition: a binary number, which is a string made up of only ones and zeros (which must be a positive number)
    Postcondition: returns the decimal value of the binary number as an int 
    """
    decValue = 0
    exponent = len(binaryNum) - 1
    for bit in binaryNum:
        bitValue = int(bit) * 2 ** exponent
        decValue += bitValue
        exponent = exponent - 1 # alternatively, exponent -= 1
    return decValue
        

#testBinaryToDecimal()
main()

Generated by GNU Enscript 1.6.6.