如何解决python中的十六进制到Base64,但我得到了错误的结果
hex_characters = {'0': '0000','1': '0001','2': '0010','3': '0011','4': '0100','5': '0101','6': '0110','7': '0111','8': '1000','9': '1001','a': '1010','b': '1011','c': '1100','d': '1101','e': '1110','f': '1111'}
b64_characters = {
'000000': 'A','000001': 'B','000010': 'C','000011': 'D','000100': 'E','000101': 'F','000110': 'G','000111': 'H','001000': 'I','001001': 'J','001010': 'K','001011': 'L','001100': 'M','001101': 'N','001110': 'O','001111': 'P','010000': 'Q','010001': 'R','010010': 'S','010011': 'T','010100': 'U','010101': 'V','010110': 'W','010111': 'X','011000': 'Y','011001': 'Z','011010': 'a','011011': 'b','011100': 'c','011101': 'd','011110': 'e','011111': 'f','100000': 'g','100001': 'h','100010': 'i','100011': 'j','100100': 'k','100101': 'l','100110': 'm','100111': 'n','101000': 'o','101001': 'p','101010': 'q','101011': 'r','101100': 's','101101': 't','101110': 'u','101111': 'v','110000': 'w','110001': 'x','110010': 'y','110011': 'z','110100': '0','110101': '1','110110': '2','110111': '3','111000': '4','111001': '5','111010': '6','111011': '7','111100': '8','111101': '9','111110': '+','111111': '/'
}
def hex_to_b64(s):
b16 = hex_characters
b64 = b64_characters
k = 0
g = 0
bin_list = list()
b64_list = list()
bin_string = ''
b64_string = ''
for i in s:
b = s[k]
bin_list.append(b16.get(b))
k += 1
a = bin_string.join(bin_list)
a_split_in_six = [(a[i:i+6]) for i in range(0,len(a),6)]
for i in a_split_in_six:
m = a_split_in_six[g]
b64_list.append(b64.get(m))
g += 1
b64_list_without_none = ['=' if x is None else x for x in b64_list]
lex = b64_string.join(b64_list_without_none)
return lex
print(hex_to_b64('4a1b48d7322394a78326cd283f0834093e65421a7e98e90e5b452d160b5d07d8'))
我写了这段代码来将一个六进制字符串转换为一个 base64 字符串,但我得到的结果是“ShtI1zIjlKeDJs0oPwg0CT5lQhp+mOkOW0UtFgtdB9=”,正确的是“ShtI1zIjlKeDJs0oPwg0CT5lQhp+mogtbgU=”(gnotfbg00)。有人能解释一下为什么我错了吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。