【问题标题】:Why do these base converters give different answers?为什么这些基础转换器给出不同的答案?
【发布时间】:2016-01-08 02:32:36
【问题描述】:

我编写了一个 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()))

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

我很确定我的程序是正确的,我不确定,但我已经进行了几次手动转换,它们与程序相比很好,但为什么这些计算器得到不同的值?

【问题讨论】:

    标签: python base base-conversion


    【解决方案1】:

    提供以许多 0 结尾的十进制数字的网站可能只是内存不足,或者使用的语言或算法不能很好地处理大数字。请记住,base 36 中的第一个 B 已经是 base 10 中的 4084512220202252076284091826176。鉴于许多人访问网站,他们必须仔细管理其资源以确保人们不会遇到延迟。该站点很可能只是限制了每个计算可以使用的内存量。

    我自己的基础转换器程序的结果:

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

    我的结果与第一个匹配。

    【讨论】:

    • 我以为那个是这样的,至于我在描述中写的那个是正确的,它与我得到的相匹配,但图片上似乎没有任何显示,也许我格式化了它错了。感谢您的回答。
    猜你喜欢
    • 2020-08-21
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多