python - Why do these base converters give different answers? -
i have written python program convert numbers positive integer base another.
print("base converter") math import log print("this converter uses 0-9, followed a-z, followed a-z, giving individual characters 0 - 61.") print("if there digits greater 61, each digit placed in pair of vertical lines, eg. 4|a|c|x|63|2|h|144|h.") def baseconvert(number, startbase, endbase): try: # make sure parameters in right format n = int(str(startbase)) + int(str(endbase)) except valueerror: return "bad base input" startbase, endbase = int(startbase), int(endbase) letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] number = str(number) in number: # make sure digits alphanumeric if not i.isalnum() , != "|": # vertical bars allowed seperate digits greater 61. other non alphanumeric characters not. return "bad input" if "|" in number: number = number.split("|") startlength = len(number) # initial length of number total = 0 # become final number in range(startlength): n = startlength - - 1 # reverses order, digit = number[n] # going last digit in number (furthest right), first. if digit in letters: digit = letters.index(digit) + 10 try: digit = int(digit) except valueerror: return "bad number input" if digit > startbase: return "bad start base" total += int(digit) * (startbase ** i) number = total if endbase > 1: n2 = int(log(number, endbase)) # length of result number elif endbase == 1: n2 = number else: print("unable complete base conversion") return total = number numbers = [] j = "" in range(n2 + 1): x = int(total/(endbase ** (n2 - i))) # starting largest place value, divides original number (now in base 10) place value if x > 9: if x < 62: x = letters[x - 10] else: x = str(x) j = "|" numbers.append(str(x)) total = total % endbase ** (n2 - i) endnumber = j.join(numbers) return endnumber # base conversion function /\ /\ # user input \/ \/ \/ \/ \/ \/ \/ print("enter number, start base , end base. (eg. 101101011, 2, 10) ") inp = "1" while inp: inp = input(">>> ") while inp.count(",") != 2: if inp == '': break print("bad input") inp = input(">>> ") if inp == '': break inp = inp.split(",") print(baseconvert(inp[0].strip(), inp[1].strip(), inp[2].strip()))
to check results right, found base converters online test against , plugged in large numbers, noticed different calculators gave different results.
i'm pretty sure program correct, i'm not sure i've done couple of manual conversions , compared program, why these calculators getting different values?
the site gives decimal number end in many 0s ran out of memory or used language or algorithm doesn't work big numbers. keep in mind first b
in base 36 4084512220202252076284091826176
in base 10. given websites accessed many people have manage resources make sure people don't experience latency. site limited amount of memory each calculation can use.
result own base converter program:
ruby base converter convert base: 36 number in base 36: bignumbertestexample convert base: 10 base 36: bignumbertestexample base 10: 4274945873844657616917501294866
my result matches first one.
Comments
Post a Comment