#---------------------------------------------------------------------- # This is how the str function works when given an int argument. #---------------------------------------------------------------------- def IntToStr(N): # N is the number for which we want a printable string. S = "" # String S is where we will be building the string result. Negative = (N < 0) # Will be True if N<0, False otherwise. N = abs(N) # Make sure N >= 0 for the while-loop. while (N > 0): # As long as N isn't zero yet... Remainder = N % 10 # ...carve off the right decimal digit of N, then... N = N // 10 # ...shift N right by one decimal digit, then... Digit = chr(ord("0") + Remainder) # ...convert the remainder digit into a character, and... S = Digit + S # ...add the character to the left end of string S. if S == "": S = "0" # If N was 0, S would still be empty, so we need to correct for that. if Negative: S = "-" + S # If N was originally negative add a minus sign to the string S. return S # Return the resulting string. #---------------------------------------------------------------------- # This is how we can convert an int N to a string for some other base # than base 10. #---------------------------------------------------------------------- def IntToStrBases(N,Base=10): S = "" Negative = (N < 0) N = abs(N) while (N > 0): # As long as N isn't zero yet... Remainder = N % Base # ...carve off the right digit of N in the target base, then... N = N // Base # ...shift N right by one digit in the target base, then... if Remainder > 9: # ...if the digit is not in 0...9, we... Digit = chr(ord("A") + Remainder - 10) # ...use the letters starting at "A" else: # ...otherwise the digit is in 0...9, so... Digit = chr(ord("0") + Remainder) # ...convert it as we did before, and... S = Digit + S # ...add the character to the left end of string S. if S == "": S = "0" if Negative: S = "-" + S return S