gpt4 book ai didi

python - 如何设置python中字符的二进制形式显示的字节数?

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:40 25 4
gpt4 key购买 nike

我希望能够将字符串中每个字符显示的字节数设置为 6。这里,字符串是 jJ,我得到的输出为 1000111001 J 给出 1001,我希望它显示 001001

a = ['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','0','1','2','3','4','5','6','7','8','9','+','/']
def mapFirst(string):
return ''.join(bin(ord(chr(a.index(c)))) for c in string).replace('0b','')

def main():
k = 'jJ'
print(mapFirst(k))

if __name__ == "__main__":
main()

最佳答案

更改:

return ''.join(bin(ord(chr(a.index(c)))) for c in string).replace('0b','')

与:

return ''.join(str(bin(ord(chr(a.index(c))))).replace('0b','').zfill(6) for c in string)

您正在寻找的函数是zfill(width)

<小时/>

注意:为了便于阅读,我建议您将该行拆分为多行。

关于python - 如何设置python中字符的二进制形式显示的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56098024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com