Contents
- ascii.py
- ascii_table.py
- binaryToDecimal.py
- decimalToBinary.py
ascii.py 1/4
[top][prev][next]
# Conversion of a text message into ASCII
# by Sara Sprenkle
print "This program converts a textual message into a sequence"
print "of numbers representing the ASCII encoding of the message."
print
message = raw_input("Please enter the message to encode: ")
print
print "Here are the ASCII codes:"
for ch in message:
print ord(ch),
print
ascii_table.py 2/4
[top][prev][next]
# Create an ASCII table
# by Sara Sprenkle
print "This program prints out the ASCII Table"
print "DEC CHAR"
print "-"*3, "-"*4
for i in xrange(33, 127):
print "%3d %4s" % (i, chr(i))
binaryToDecimal.py 3/4
[top][prev][next]
# Converts a binary number into a decimal
# By CSCI111, 02.16.2011
# Read in the binary number as a string -- why?
num = raw_input("Enter the binary #: ")
# accumulate the decimal value in this variable
decVal = 0
# go through the positions in the string
for pos in xrange(len(num)):
# num[pos] is a string; need to convert to an int
bit = int(num[pos])
# calculate which "place" the current bit is at
place = 2**(len(num)-pos-1)
# add to the decimal value
decVal += place * bit
print "The decimal value for", num, "is", decVal
decimalToBinary.py 4/4
[top][prev][next]
# Converts a decimal number into a binary number
# By CSCI111, 02.16.2011
#Read in the decimal as an integer
decInput = input("Enter the decimal number: ")
#Initialize the result to the empty string
result = ""
# save the original input for use in the output
dec = decInput
if dec == 0:
# need to handle if the number is 0;
# the while loop won't handle it appropriately
result = "0"
else:
#Repeat until the decimal is 0:
while dec != 0:
result = str(dec % 2) + result
dec = dec / 2
#Display the result
print "The decimal number", decInput, "is the binary number", result
Generated by GNU enscript 1.6.4.