CTF中一點進制轉換腳本記錄

第一版進制轉換: 

# coding=utf-8
# --author:valecalida--
# 系統只能轉字符串,無法轉數字!!!
import sys


def encode_string2base():
    str_input = input("Please input a string:")
    result = ''
    for i in range(len(str_input)):
        key = ord(str_input[i])
        if key % 4 == 0:
            result += str(bin(key)) + " "
        elif key % 4 == 1:
            result += str(oct(key)) + " "
        elif key % 4 == 2:
            result += "0d" + str(key) + " "
        elif key % 4 == 3:
            result += str(hex(key)) + " "
        else:
            pass
    print("字符串轉二、八、十、十六進制的結果是:", result)


def decode_base2basestring():
    s = input("Please input the bases you want to change:")
    keys = s.split(" ")
    result = ""
    for key in keys:
        if key[0:2] == '0b':
            result += str(chr(int(key[2:], 2)))
        elif key[0:2] == '0o':
            result += str(chr(int(key[2:], 8)))
        elif key[0:2] == '0x':
            result += str(chr(int(key[2:], 16)))
        elif key[0:2] == '0d':
            result += str(chr(int(key[2:])))
        else:
            result += ""
    print("各種進制轉換爲字符串的結果是:\n\t\t", result)


def ascii2bin():
    s = input("Please input the bases you want to change:")
    result = ""
    for key in s:
        result += str(bin(ord(key))) + " "
    print("字符串轉二進制的結果是:\n\t\t", result)


def bin2ascii():
    s = input("輸入你想轉變成字符串的二進制數據(最好以空格間隔開):")
    result = ""
    keys = s.split(" ")
    for key in keys:
        if key[0:2] == '0b':
            result += str(chr(int(key[2:], 2)))
        else:
            print("您輸入的好像有一部分不是二進制數據")
    print(result)


def ascii2oct():
    s = input("Please input the bases you want to change:")
    result = ""
    for key in s:
        result += str(oct(ord(key))) + " "
    print("字符串轉八進制的結果是:\n\t\t", result)


def oct2ascii():
    s = input("輸入你想轉變成字符串的八進制數據(最好以空格間隔開):")
    result = ""
    keys = s.split(" ")
    for key in keys:
        if key[0:2] == '0o':
            result += str(chr(int(key[2:], 8)))
        else:
            print("您輸入的好像有一部分不是八進制數據")
    print(result)

def ascii2hex():
    s = input("Please input the bases you want to change:")
    result = ""
    for key in s:
        result += str(hex(ord(key))) + " "
    print("字符串轉十六進制的結果是:\n\t\t", result)


def hex2ascii():
    s = input("輸入你想轉變成字符串的十六進制數據(最好以空格間隔開):")
    result = ""
    keys = s.split(" ")
    for key in keys:
        if key[0:2] == '0x':
            result += str(chr(int(key[2:], 16)))
        else:
            print("您輸入的好像有一部分不是十六進制數據")
    print(result)


def ascii2dec():
    s = input("Please input the bases you want to change:")
    result = ""
    for key in s:
        result += str((ord(key))) + " "
    print("字符串轉十進制的結果是:\n\t\t", result)


def dec2ascii():
    s = input("輸入你想轉變成字符串的十進制數據(最好以空格間隔開):")
    result = ""
    keys = s.split(" ")
    for key in keys:
        key = int(key)
        print("key is: ", key)
        if key >= 1 and key <= 128:
            result += str(chr(int(key))) + " "
        else:
            print("您輸入的好像有一部分不是十進制數據")
    print(result)


def menu():
    print("*" * 60)
    print("*\t\t(1)將字符串按照二、八、十六、十進制的順序轉換    *")
    print("*\t\t(2)將二、八、十六、十進制的方式轉換成字符串      *")
    print("*\t\t(3)將字符串轉換成二進制                          *")
    print("*\t\t(4)將二進制轉換成字符串                          *")
    print("*\t\t(5)將字符串轉換成八進制                          *")
    print("*\t\t(6)將八進制轉換成字符串                          *")
    print("*\t\t(7)將字符串轉換成十六進制                        *")
    print("*\t\t(8)將十六進制轉換成字符串                        *")
    print("*\t\t(9)將字符串轉換成十進制(ASCII碼)               *")
    print("*\t\t(10)將十進制轉換成字符串(帶0d格式)             *")
    print("*\t\t(q)退出轉換系統                                  *")
    print("*" * 60)


def run_system():
    while True:
        menu()
        user_choice = input("Please input the number you want to operate: ")
        if user_choice == '1':
            encode_string2base()
        elif user_choice == '2':
            decode_base2basestring()
        elif user_choice == '3':
            ascii2bin()
        elif user_choice == '4':
            bin2ascii()
        elif user_choice == '5':
            ascii2oct()
        elif user_choice == '6':
            oct2ascii()
        elif user_choice == '7':
            ascii2hex()
        elif user_choice == '8':
            hex2ascii()
        elif user_choice == '9':
            ascii2dec()
        elif user_choice == '10':
            dec2ascii()
        elif user_choice == 'q':
            print("Quiting this system...")
            sys.exit()
        else:
            print("Wrong input!!!  |  Please make sure your input is right!\n")


run_system()

下面是一部分運行結果

************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 1
Please input a string:1234567890-=qwertyuiop[]\asdfghjkl;zxcvbnm,./
字符串轉二、八、十、十六進制的結果是: 0o61 0d50 0x33 0b110100 0o65 0d54 0x37 0b111000 0o71 0b110000 0o55 0o75 0o161 0x77 0o145 0d114 0b1110100 0o171 0o165 0o151 0x6f 0b1110000 0x5b 0o135 0b1011100 0o141 0x73 0b1100100 0d102 0x67 0b1101000 0d106 0x6b 0b1101100 0x3b 0d122 0b1111000 0x63 0d118 0d98 0d110 0o155 0b101100 0d46 0x2f
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 2
Please input the bases you want to change:0o61 0d50 0x33 0b110100 0o65 0d54 0x37 0b111000 0o71 0b110000 0o55 0o75 0o161 0x77 0o145 0d114 0b1110100 0o171 0o165 0o151 0x6f 0b1110000 0x5b 0o135 0b1011100 0o141 0x73 0b1100100 0d102 0x67 0b1101000 0d106 0x6b 0b1101100 0x3b 0d122 0b1111000 0x63 0d118 0d98 0d110 0o155 0b101100 0d46 0x2f
各種進制轉換爲字符串的結果是:
                 1234567890-=qwertyuiop[]\asdfghjkl;zxcvbnm,./
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 3
Please input the bases you want to change:qwertyuiop
字符串轉二進制的結果是:
                 0b1110001 0b1110111 0b1100101 0b1110010 0b1110100 0b1111001 0b1110101 0b1101001 0b1101111 0b1110000
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 4
輸入你想轉變成字符串的二進制數據(最好以空格間隔開):0b1110001 0b1110111 0b1100101 0b1110010 0b1110100 0b1111001 0b1110101 0b1101001 0b1101111 0b1110000
qwertyuiop
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 4
輸入你想轉變成字符串的二進制數據(最好以空格間隔開):qwertyuiop
您輸入的好像有一部分不是二進制數據

************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 5
Please input the bases you want to change:qwertyuiop
字符串轉八進制的結果是:
                 0o161 0o167 0o145 0o162 0o164 0o171 0o165 0o151 0o157 0o160
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 6
輸入你想轉變成字符串的八進制數據(最好以空格間隔開):0o161 0o167 0o145 0o162 0o164 0o171 0o165 0o151 0o157 0o160
qwertyuiop
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 7
Please input the bases you want to change:qwertyuiop
字符串轉十六進制的結果是:
                 0x71 0x77 0x65 0x72 0x74 0x79 0x75 0x69 0x6f 0x70
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 8
輸入你想轉變成字符串的十六進制數據(最好以空格間隔開):qwertyuiop
您輸入的好像有一部分不是十六進制數據

************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 8
輸入你想轉變成字符串的十六進制數據(最好以空格間隔開):0x71 0x77 0x65 0x72 0x74 0x79 0x75 0x69 0x6f 0x70
qwertyuiop
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 9
Please input the bases you want to change:8
字符串轉十進制的結果是:
                 56
************************************************************
*               (1)將字符串按照二、八、十六、十進制的順序轉換    *
*               (2)將二、八、十六、十進制的方式轉換成字符串      *
*               (3)將字符串轉換成二進制                          *
*               (4)將二進制轉換成字符串                          *
*               (5)將字符串轉換成八進制                          *
*               (6)將八進制轉換成字符串                          *
*               (7)將字符串轉換成十六進制                        *
*               (8)將十六進制轉換成字符串                        *
*               (9)將字符串轉換成十進制(ASCII碼)               *
*               (10)將十進制轉換成字符串(帶0d格式)             *
*               (q)退出轉換系統                                  *
************************************************************
Please input the number you want to operate: 10
輸入你想轉變成字符串的十進制數據(最好以空格間隔開):56
key is:  56
8

第二版進制轉換:

我感覺上面寫的好像有些雜亂,所以縮減了一部分內容,加了一點異常處理,下面是新的代碼

# coding=utf-8
#--author:valecalida--
import sys


def encode_string2base():
    """
    這個函數的功能是將字符串轉換成以0b、0o、0d、0x爲首的二、八、十、十六進制的數據
    """
    str_input = input("請將需要轉換的字符串輸入: ")
    result = ''
    for i in range(len(str_input)):
        key = ord(str_input[i])
        if key % 4 == 0:
            result += str(bin(key)) + " "
        elif key % 4 == 1:
            result += str(oct(key)) + " "
        elif key % 4 == 2:
            result += "0d" + str(key) + " "
        elif key % 4 == 3:
            result += str(hex(key)) + " "
        else:
            pass
    print("字符串轉二、八、十、十六進制的結果是:", result)


def decode_base2basestring():
    """
    這個函數的功能是將以0b、0o、0d、0x爲首的各種格式的數據轉換成字符串
    :return:
    """
    s = input("請將以0b、0o、0d、0x爲首的各種格式的數據輸入:")
    keys = s.split(" ")
    result = ""
    for key in keys:
        if key[0:2] == '0b':
            result += str(chr(int(key[2:], 2)))
        elif key[0:2] == '0o':
            result += str(chr(int(key[2:], 8)))
        elif key[0:2] == '0x':
            result += str(chr(int(key[2:], 16)))
        elif key[0:2] == '0d':
            result += str(chr(int(key[2:])))
        else:
            result += ""
    print("各種進制轉換爲字符串的結果是:", result)
    s = result
    result1 = ""
    result2 = ""
    result3 = ""
    result4 = ""
    for key in s:
        result1 += str(bin(ord(key))) + " "
        result2 += str(oct(ord(key))) + " "
        result3 += str(hex(ord(key))) + " "
        result4 += str(ord(key)) + " "
    print("字符串轉二進制的結果是:\t", result1)
    print("字符串轉八進制的結果是:\t", result2)
    print("字符串轉十進制的結果是:\t", result4)
    print("字符串轉十六進制的結果是:\t", result3)


def strings2bases():
    """
    這個函數的功能是將字符串轉換成二、八、十六進制輸出出來
    :return:
    """
    s = input("請將您想轉換的字符串輸入:")
    result1 = ""
    result2 = ""
    result3 = ""
    result4 = ""
    for key in s:
        result1 += str(bin(ord(key))) + " "
        result2 += str(oct(ord(key))) + " "
        result3 += str(hex(ord(key))) + " "
        result4 += str(ord(key)) + " "
    print("字符串轉二進制的結果是:\t", result1)
    print("字符串轉八進制的結果是:\t", result2)
    print("字符串轉十進制的結果是:\t", result4)
    print("字符串轉十六進制的結果是:\t", result3)


def num2bases():
    """這個函數的功能十將數字轉換成各種進制"""
    res1 = ""
    res2 = ""
    res3 = ""
    try:
        s = int(input("請輸入十進制的數據:"))
    except:
        print("您的輸入我好像不太能理解,請再重新嘗試一下吧。")
    else:
        print("您的輸入是:", s)
        print("對應的二進制是:", bin(s))
        print("對應的八進制是:", oct(s))
        print("對應的十六進制是:", hex(s))


def menu():
    print("*" * 80)
    print("*\t\t(1)將字符串轉換成二、八、十、十六進制的形式                          *")
    print("*\t\t(2)將二、八、十、十六進制的形式轉換成字符串並顯示各種形式的進制      *")
    print("*\t\t(3)將字符串轉換成各種單獨的進制形式                                  *")
    print("*\t\t(4)將數字轉換成各種單獨的進制形式                                    *")
    print("*" * 80)


def run_system():
    while True:
        menu()
        user_choice = input("Please input the number you want to operate: ")
        if user_choice == '1':
            encode_string2base()
            print("")
        elif user_choice == '2':
            decode_base2basestring()
            print("")
        elif user_choice == '3':
            strings2bases()
            print("")
        elif user_choice == '4':
            num2bases()
            print("")
        elif user_choice == 'q':
            sys.exit()
        else:
            print("您的輸入看起來我好像理解不了,請重新嘗試一下吧\n")

run_system()

 下面是測試結果:


********************************************************************************
*		(1)將字符串轉換成二、八、十、十六進制的形式                          *
*		(2)將二、八、十、十六進制的形式轉換成字符串並顯示各種形式的進制      *
*		(3)將字符串轉換成各種單獨的進制形式                                  *
*		(4)將數字轉換成各種單獨的進制形式                                    *
********************************************************************************
Please input the number you want to operate: 1
請將需要轉換的字符串輸入: 1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./
字符串轉二、八、十、十六進制的結果是: 0o61 0d50 0x33 0b110100 0o65 0d54 0x37 0b111000 0o71 0b110000 0o55 0o75 0o161 0x77 0o145 0d114 0b1110100 0o171 0o165 0o151 0x6f 0b1110000 0x5b 0o135 0b1011100 0o141 0x73 0b1100100 0d102 0x67 0b1101000 0d106 0x6b 0b1101100 0x3b 0x27 0d122 0b1111000 0x63 0d118 0d98 0d110 0o155 0b101100 0d46 0x2f 

********************************************************************************
*		(1)將字符串轉換成二、八、十、十六進制的形式                          *
*		(2)將二、八、十、十六進制的形式轉換成字符串並顯示各種形式的進制      *
*		(3)將字符串轉換成各種單獨的進制形式                                  *
*		(4)將數字轉換成各種單獨的進制形式                                    *
********************************************************************************
Please input the number you want to operate: 2
請將以0b、0o、0d、0x爲首的各種格式的數據輸入:0o61 0d50 0x33 0b110100 0o65 0d54 0x37 0b111000 0o71 0b110000 0o55 0o75 0o161 0x77 0o145 0d114 0b1110100 0o171 0o165 0o151 0x6f 0b1110000 0x5b 0o135 0b1011100 0o141 0x73 0b1100100 0d102 0x67 0b1101000 0d106 0x6b 0b1101100 0x3b 0x27 0d122 0b1111000 0x63 0d118 0d98 0d110 0o155 0b101100 0d46 0x2f 
各種進制轉換爲字符串的結果是: 1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./
字符串轉二進制的結果是:	 0b110001 0b110010 0b110011 0b110100 0b110101 0b110110 0b110111 0b111000 0b111001 0b110000 0b101101 0b111101 0b1110001 0b1110111 0b1100101 0b1110010 0b1110100 0b1111001 0b1110101 0b1101001 0b1101111 0b1110000 0b1011011 0b1011101 0b1011100 0b1100001 0b1110011 0b1100100 0b1100110 0b1100111 0b1101000 0b1101010 0b1101011 0b1101100 0b111011 0b100111 0b1111010 0b1111000 0b1100011 0b1110110 0b1100010 0b1101110 0b1101101 0b101100 0b101110 0b101111 
字符串轉八進制的結果是:	 0o61 0o62 0o63 0o64 0o65 0o66 0o67 0o70 0o71 0o60 0o55 0o75 0o161 0o167 0o145 0o162 0o164 0o171 0o165 0o151 0o157 0o160 0o133 0o135 0o134 0o141 0o163 0o144 0o146 0o147 0o150 0o152 0o153 0o154 0o73 0o47 0o172 0o170 0o143 0o166 0o142 0o156 0o155 0o54 0o56 0o57 
字符串轉十進制的結果是:	 49 50 51 52 53 54 55 56 57 48 45 61 113 119 101 114 116 121 117 105 111 112 91 93 92 97 115 100 102 103 104 106 107 108 59 39 122 120 99 118 98 110 109 44 46 47 
字符串轉十六進制的結果是:	 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x30 0x2d 0x3d 0x71 0x77 0x65 0x72 0x74 0x79 0x75 0x69 0x6f 0x70 0x5b 0x5d 0x5c 0x61 0x73 0x64 0x66 0x67 0x68 0x6a 0x6b 0x6c 0x3b 0x27 0x7a 0x78 0x63 0x76 0x62 0x6e 0x6d 0x2c 0x2e 0x2f 

********************************************************************************
*		(1)將字符串轉換成二、八、十、十六進制的形式                          *
*		(2)將二、八、十、十六進制的形式轉換成字符串並顯示各種形式的進制      *
*		(3)將字符串轉換成各種單獨的進制形式                                  *
*		(4)將數字轉換成各種單獨的進制形式                                    *
********************************************************************************
Please input the number you want to operate: 3
請將您想轉換的字符串輸入:valecalida
字符串轉二進制的結果是:	 0b1110110 0b1100001 0b1101100 0b1100101 0b1100011 0b1100001 0b1101100 0b1101001 0b1100100 0b1100001 
字符串轉八進制的結果是:	 0o166 0o141 0o154 0o145 0o143 0o141 0o154 0o151 0o144 0o141 
字符串轉十進制的結果是:	 118 97 108 101 99 97 108 105 100 97 
字符串轉十六進制的結果是:	 0x76 0x61 0x6c 0x65 0x63 0x61 0x6c 0x69 0x64 0x61 

********************************************************************************
*		(1)將字符串轉換成二、八、十、十六進制的形式                          *
*		(2)將二、八、十、十六進制的形式轉換成字符串並顯示各種形式的進制      *
*		(3)將字符串轉換成各種單獨的進制形式                                  *
*		(4)將數字轉換成各種單獨的進制形式                                    *
********************************************************************************
Please input the number you want to operate: 4
請輸入十進制的數據:123
您的輸入是: 123
對應的二進制是: 0b1111011
對應的八進制是: 0o173
對應的十六進制是: 0x7b

********************************************************************************
*		(1)將字符串轉換成二、八、十、十六進制的形式                          *
*		(2)將二、八、十、十六進制的形式轉換成字符串並顯示各種形式的進制      *
*		(3)將字符串轉換成各種單獨的進制形式                                  *
*		(4)將數字轉換成各種單獨的進制形式                                    *
********************************************************************************
Please input the number you want to operate: 4
請輸入十進制的數據:mbnvctyhui
您的輸入我好像不太能理解,請再重新嘗試一下吧。

再追加一個數字進行轉換的

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# --author:valecalida--
# Edit_Time : 2019/12/14 16:53
import binascii
conversion = input("輸入要轉換的進制:")
area0 = ['0', '1']
area1 = ['0', '1', '2', '3', '4', '5', '6', '7']
area2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
area3 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']


def detect_conversion():
    conversions = conversion.upper()
    res = ""
    i = max(conversions)
    if i in area0 or conversion[0:2]=='0b':
        res = '1'
    elif i in area1 or conversion[0:2]=='0o':
        res = '2'
    elif i in area2 or conversion[0:2]=='0d':
        res = '3'
    elif i in area3 or conversion[0:2]=='0x':
        res = '4'
    else:
        res = '5'
    return res


def binary2strings():
    half = hex(int(conversion, base=2))[2:]
    strings = binascii.a2b_hex(bytes(half, 'utf-8'))
    print(strings)


def dec2strings():
    half = hex(int(conversion))[2:]
    strings = binascii.a2b_hex(half)
    print(strings)


def oct2strings():
    half = hex(int(conversion,base=8))[2:]
    strings = binascii.a2b_hex(bytes(half, 'utf-8'))
    print(strings)


def hex2strings():
    strings = binascii.a2b_hex(bytes(conversion,'utf-8'))
    print(strings)


def main():
    if detect_conversion() == '1':
        print("這是一個二進制字符串")
        binary2strings()
    elif detect_conversion() == '2':
        print("這是一個八進制字符串")
        oct2strings()
    elif detect_conversion() == '3':
        print("這是一個十進制字符串")
        dec2strings()
    elif detect_conversion() == '4':
        print("這是一個十六進制字符串")
        hex2strings()
    elif detect_conversion() == '5':
        print("這可能只是個字符串,沒有具體的進制")


if __name__ == '__main__':
    main()

 

發佈了83 篇原創文章 · 獲贊 42 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章