首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:“BitStream”对象不能解释为整数

TypeError:“BitStream”对象不能解释为整数
EN

Stack Overflow用户
提问于 2021-05-14 00:34:13
回答 1查看 27关注 0票数 0

我是python世界的新手,我得到了一个我不确定的输入错误:

代码语言:javascript
复制
...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

有什么想法吗?这是有问题的代码行的星号:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2021-05-14 01:24:53

你给十六进制函数一个BitStream参数,而不是整数。此外,在while循环中使用stream.length也不会减少。

但是你可以使用下面的代码来encodeb64。

代码语言:javascript
复制
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‘format
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67522868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档