<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>A put a HEX on you</title>
</head>
<body>
<form action="converttohex.py" >
<p> This form will convert your age from base 10 (decimal) to base 16 (hexadecimal) </p>
<p> What is your name: <input type="text" name="yourname" /></p>
<p> What is your age : <input type="text" name="yourage" size="3" maxlength="3" /></p>
<p> <input type="submit" value="Convert to Hex!" name="button1" /></p>
</form>
</body>
</html>
import cgi
form = cgi.FieldStorage()
# convert an integer that is between 0 and 15 into a hexadecimal character
def strhex(x):
if x < 10:
return str(x)
elif x == 10:
return "A"
elif x == 11:
return "B"
elif x == 12:
return "C"
elif x == 13:
return "D"
elif x == 14:
return "E"
else: # must be 15
return "F"
print """Content-type: text/html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x
html1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Your age in hex</title>
</head>
<body>
"""
age = int(form["yourage"].value)
first_digit = age / 16 # what integer do you get when dividing age by 16
second_digit = age % 16 # and what remainder do you get
print "<p>"
print form["yourname"].value + " has a hex age of "
# print str(first_digit) + str(strhex(second_digit)) # what I originally had
# which was then modified
print "\"" + strhex(first_digit) + strhex(second_digit) + "\""
print "</p>"
print """
<hr class="footsep" />
<p class="footlink">Return to <a href="./">the index</a>.</p>
</body>
</html>