Hướng dẫn any base to any base conversion in python - bất kỳ cơ sở nào thành bất kỳ chuyển đổi cơ sở nào trong python

Đây phải là nửa đầu của câu trả lời cho vấn đề của bạn. Bạn có thể tìm ra cách chuyển đổi thành một cơ sở?

# Create a symbol-to-value table.
SY2VA = {'0': 0,
         '1': 1,
         '2': 2,
         '3': 3,
         '4': 4,
         '5': 5,
         '6': 6,
         '7': 7,
         '8': 8,
         '9': 9,
         'A': 10,
         'B': 11,
         'C': 12,
         'D': 13,
         'E': 14,
         'F': 15,
         'G': 16,
         'H': 17,
         'I': 18,
         'J': 19,
         'K': 20,
         'L': 21,
         'M': 22,
         'N': 23,
         'O': 24,
         'P': 25,
         'Q': 26,
         'R': 27,
         'S': 28,
         'T': 29,
         'U': 30,
         'V': 31,
         'W': 32,
         'X': 33,
         'Y': 34,
         'Z': 35,
         'a': 36,
         'b': 37,
         'c': 38,
         'd': 39,
         'e': 40,
         'f': 41,
         'g': 42,
         'h': 43,
         'i': 44,
         'j': 45,
         'k': 46,
         'l': 47,
         'm': 48,
         'n': 49,
         'o': 50,
         'p': 51,
         'q': 52,
         'r': 53,
         's': 54,
         't': 55,
         'u': 56,
         'v': 57,
         'w': 58,
         'x': 59,
         'y': 60,
         'z': 61,
         '!': 62,
         '"': 63,
         '#': 64,
         '$': 65,
         '%': 66,
         '&': 67,
         "'": 68,
         '(': 69,
         ')': 70,
         '*': 71,
         '+': 72,
         ',': 73,
         '-': 74,
         '.': 75,
         '/': 76,
         ':': 77,
         ';': 78,
         '<': 79,
         '=': 80,
         '>': 81,
         '?': 82,
         '@': 83,
         '[': 84,
         '\\': 85,
         ']': 86,
         '^': 87,
         '_': 88,
         '`': 89,
         '{': 90,
         '|': 91,
         '}': 92,
         '~': 93}

# Take a string and base to convert to.
# Allocate space to store your number.
# For each character in your string:
#     Ensure character is in your table.
#     Find the value of your character.
#     Ensure value is within your base.
#     Self-multiply your number with the base.
#     Self-add your number with the digit's value.
# Return the number.

def str2int(string, base):
    integer = 0
    for character in string:
        assert character in SY2VA, 'Found unknown character!'
        value = SY2VA[character]
        assert value < base, 'Found digit outside base!'
        integer *= base
        integer += value
    return integer

Đây là nửa sau của giải pháp. Bằng cách sử dụng hai chức năng này, việc chuyển đổi cơ sở rất dễ thực hiện.

# Create a value-to-symbol table.
VA2SY = dict(map(reversed, SY2VA.items()))

# Take a integer and base to convert to.
# Create an array to store the digits in.
# While the integer is not zero:
#     Divide the integer by the base to:
#         (1) Find the "last" digit in your number (value).
#         (2) Store remaining number not "chopped" (integer).
#     Save the digit in your storage array.
# Return your joined digits after putting them in the right order.

def int2str(integer, base):
    array = []
    while integer:
        integer, value = divmod(integer, base)
        array.append(VA2SY[value])
    return ''.join(reversed(array))

Sau khi kết hợp tất cả lại với nhau, bạn nên kết thúc với chương trình dưới đây. Hãy dành thời gian để tìm ra nó!

innitvar = raw_input("Please enter a number: ")
basevar = int(raw_input("Please enter the base that your number is in: "))
convertvar = int(raw_input("Please enter the base that you would like to convert to: "))

# Create a symbol-to-value table.
SY2VA = {'0': 0,
         '1': 1,
         '2': 2,
         '3': 3,
         '4': 4,
         '5': 5,
         '6': 6,
         '7': 7,
         '8': 8,
         '9': 9,
         'A': 10,
         'B': 11,
         'C': 12,
         'D': 13,
         'E': 14,
         'F': 15,
         'G': 16,
         'H': 17,
         'I': 18,
         'J': 19,
         'K': 20,
         'L': 21,
         'M': 22,
         'N': 23,
         'O': 24,
         'P': 25,
         'Q': 26,
         'R': 27,
         'S': 28,
         'T': 29,
         'U': 30,
         'V': 31,
         'W': 32,
         'X': 33,
         'Y': 34,
         'Z': 35,
         'a': 36,
         'b': 37,
         'c': 38,
         'd': 39,
         'e': 40,
         'f': 41,
         'g': 42,
         'h': 43,
         'i': 44,
         'j': 45,
         'k': 46,
         'l': 47,
         'm': 48,
         'n': 49,
         'o': 50,
         'p': 51,
         'q': 52,
         'r': 53,
         's': 54,
         't': 55,
         'u': 56,
         'v': 57,
         'w': 58,
         'x': 59,
         'y': 60,
         'z': 61,
         '!': 62,
         '"': 63,
         '#': 64,
         '$': 65,
         '%': 66,
         '&': 67,
         "'": 68,
         '(': 69,
         ')': 70,
         '*': 71,
         '+': 72,
         ',': 73,
         '-': 74,
         '.': 75,
         '/': 76,
         ':': 77,
         ';': 78,
         '<': 79,
         '=': 80,
         '>': 81,
         '?': 82,
         '@': 83,
         '[': 84,
         '\\': 85,
         ']': 86,
         '^': 87,
         '_': 88,
         '`': 89,
         '{': 90,
         '|': 91,
         '}': 92,
         '~': 93}

# Take a string and base to convert to.
# Allocate space to store your number.
# For each character in your string:
#     Ensure character is in your table.
#     Find the value of your character.
#     Ensure value is within your base.
#     Self-multiply your number with the base.
#     Self-add your number with the digit's value.
# Return the number.

integer = 0
for character in innitvar:
    assert character in SY2VA, 'Found unknown character!'
    value = SY2VA[character]
    assert value < basevar, 'Found digit outside base!'
    integer *= basevar
    integer += value

# Create a value-to-symbol table.
VA2SY = dict(map(reversed, SY2VA.items()))

# Take a integer and base to convert to.
# Create an array to store the digits in.
# While the integer is not zero:
#     Divide the integer by the base to:
#         (1) Find the "last" digit in your number (value).
#         (2) Store remaining number not "chopped" (integer).
#     Save the digit in your storage array.
# Return your joined digits after putting them in the right order.

array = []
while integer:
    integer, value = divmod(integer, convertvar)
    array.append(VA2SY[value])
answer = ''.join(reversed(array))

# Display the results of the calculations.
print answer

Làm thế nào để bạn chuyển đổi một cơ sở thành một cơ sở trong Python?

Cách dễ nhất (có lẽ) là chuyển đổi bất kỳ số nào từ cơ sở B1 sang B2 là chuyển đổi B1 → thập phân → B2. Vì vậy, để chuyển đổi từ bất kỳ cơ sở nào sang số thập phân, hãy tìm tổng của tất cả [chữ số*(cơ sở^nguồn)] (trong đó công suất là 0 thành [numofdigits-1]) theo thứ tự ngược của các chữ số.convert b1→Decimal→b2. So, to convert from any base to Decimal, find the sum of all [digit*(base^power)] (where power is 0 to [NumOfDigits-1]) in the reverse order of the digits.

Làm thế nào để bạn chuyển đổi sang một cơ sở khác trong Python?

Có một cách để chuyển đổi sang các cơ sở khác nhau bằng cách chia số cơ sở 10 cho cơ sở và lấy phần còn lại và lặp lại cho đến khi nó không chia hết và viết thương số theo sau là tất cả các phần còn lại.(Vd.dividing the base 10 number by the base and taking the remainder and repeating until it is not divisible and write the quotient followed by all the remainders. (EX. 50/6 = 8 R 2, 8/6 = 1 R2 so 50 in base 6 would be 122).

Các bước để chuyển đổi bất kỳ cơ sở nào thành bất kỳ cơ sở nào là gì?

Bước 1 - Chia số thập phân sẽ được chuyển đổi bởi giá trị của cơ sở mới.Bước 2 - Lấy phần còn lại từ Bước 1 là chữ số ngoài cùng bên phải (chữ số ít có ý nghĩa nhất) của số cơ sở mới.Bước 3 - Chia thương số của phân chia trước đó cho cơ sở mới.

Làm thế nào để bạn chuyển đổi cơ sở 2 thành cơ sở 10 trong Python?

Do đó, chuyển đổi nhị phân sang thập phân trong Python, nhị phân (cơ sở-2) (0 1 0 1 1) 2 (01011) _2 (01011) 2 tương đương với (1 1) 1 0 (11) _ {10} (11)Số thập phân (cơ sở-10).