gpt4 book ai didi

python - 如何在python中将位转换为字节?

转载 作者:太空宇宙 更新时间:2023-11-04 08:26:58 33 4
gpt4 key购买 nike

我正在使用以下代码将字符转换为位,但我不知道如何将这些位转换回它们的字符。

我尝试按照我采取的步骤来逆转这个过程。我知道 ord() 的反义词是 chr(),但是如何反转 format(ord(char),"b")?感谢任何帮助

temp = format(ord(char), 'b')

最佳答案

您可以使用 int() 将字符串转换回整数通过 2 的基地然后返回到带有 chr() 的字符:

temp = format(ord('a'), 'b')
print(temp)
#'1100001'

c = chr(int(temp, 2))
print(c)
# 'a'

关于python - 如何在python中将位转换为字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204005/

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