# function to convert from hexidecimal string to integer: def hexdec(H): i = len(H)-1; # loop counter, start at last character dec = 0; # decimal number to be computed digit=0 # variable to hold dec value for each digit power = 0 # the power of 16 currently being computed while i>=0: # first convert character to a numerical value ac = ord(H[i]) # ac is ascii code of character if (ac>=48 and ac<=57): # digits 0-9: digit = ac-48 else: if (ac>=65 and ac<=70): # digits A-F digit = 10 + (ac-65) else: raise Exception("invalid input: ",H) # abort program # at this point digit is from 0 to 15 dec = dec + digit*(16**power) power = power+1 i = i-1 #end while return dec #end hexdec print hexdec("4F") print hexdec("E0D8") print hexdec("00FF") # function to convert from integer to hexidecimal string def dechex(D): digits = "0123456789ABCDEF" # string to hold all hex digits Hex = "" # return value: constructed char by char if (D==0): return "0" # special case while (D!=0): dv = D % 16 # value of one digit 0-15 hv = digits[dv] # corresponding hex digit: 0-F Hex = hv+Hex # add char to left end of string D = D/16 # go to next digit #end while return Hex # end dechex print "\n", dechex(72) print dechex(255) print dechex(65535) print dechex(400)