如何解决类型错误:“BitStream”对象不能解释为整数
我是 Python 世界的新手,我遇到了一个我不确定的输入错误:
...File "/home/simon/python/piptroubleshoot/Main.py",line 15,in main
hexToBase64(u'49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d')
File "/home/simon/python/piptroubleshoot/Main.py",line 10,in hexToBase64
base64.b64encode(hex(stream.read(4)))
TypeError: 'BitStream' object cannot be interpreted as an integer
有什么想法吗?这是有问题的行加星标的代码:
def hexToBase64(i: hex):
stream = bitstring.BitStream(hex=i)
while stream.length > 0:
**base64.b64encode(hex(stream.read(4)))**
def main():
""" Main program """
print(u'49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d')
hexToBase64(u'49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d')
return 0
if __name__ == "__main__":
main()
解决方法
您给十六进制函数一个 BitStream 参数而不是 Integer。此外,在 while 循环中使用 stream.length 也不会减少。
但是你可以使用下面的代码来编码b64。
def hexToBase64(i: hex):
stream = bitstring.BitStream(hex=i)
base64.b64encode((stream.read(stream.length).hex.encode()))
-
stream.read(stream.length) -> 读取全部
-
.hex -> 转换为十六进制字符串
-
.encode() -> 使其成为 b'xxxx' 格式
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。