为什么这些基础转换器给出不同的答案?

时间:2015-10-10 04:10:42

标签: python base base-conversion

我编写了一个python程序,用于将数字从任何正整数基数转换为另一个。

print("Base Converter")
from math import log
print("This converter uses 0-9, followed by A-Z, followed by a-z, giving it individual characters from 0 - 61.")
print("If there are digits greater than 61, each digit will be placed in a pair of vertical lines, eg. 4|A|c|X|63|2|H|144|H.")
def BaseConvert(number, StartBase, EndBase):
    try: # Make sure that the parameters are in the 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)
    for i in number: # Make sure that all the digits are alphanumeric
        if not i.isalnum() and i != "|": # Vertical bars are allowed as they seperate digits greater than 61. Other non alphanumeric characters are not.
            return "Bad input"
    if "|" in number:
        number = number.split("|")
    StartLength = len(number) # Initial length of number
    total = 0 # This will become the final number
    for i in range(StartLength):
        n = StartLength - i - 1 # This effectively reverses the order,
        Digit = number[n]       # going from the last digit in the number (furthest to the right), to the 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 to complete base conversion")
        return
    total = number
    numbers = []
    J = ""
    for i in range(n2 + 1):
        X = int(total/(EndBase ** (n2 - i))) # Starting from the largest place value, divides the original number (Now in base 10) by the 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 your number, start base and 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()))

为了检查我的结果是否正确,我发现一些在线基站转换器进行测试并插入大量数据,但注意到不同的计算器给出了不同的结果。

我非常确定我的程序是正确的,我不是很确定,但我已经完成了几次手动转换,并且他们与程序进行了比较,但为什么这些计算器会有所不同值?

Taken from: http://www.kaagaard.dk/service/convert.htm Not sure where I got this one, but this conversion is actually correct. Taken from: http://baseconvert.com

1 个答案:

答案 0 :(得分:1)

以十进制数结束的网站可能只是内存不足或者使用的语言或算法对于大数字而言效果不佳。请注意,只有基数36中的第一个B已经在基数10中4084512220202252076284091826176。鉴于网站被许多人访问,他们必须仔细管理他们的资源,以确保人们不要体验延迟。该网站很可能只限制了每个计算可以使用的内存量。

来自我自己的基本转换程序的结果:

Ruby Base Converter
Convert from base: 36
Number in base 36: BIGNUMBERTESTEXAMPLE
Convert to base: 10
Base 36: BIGNUMBERTESTEXAMPLE
Base 10: 4274945873844657616917501294866

我的结果与第一个匹配。