【问题标题】:struct.pack and struct.unpack only doing the first character of a stringstruct.pack 和 struct.unpack 只做字符串的第一个字符
【发布时间】:2020-08-13 21:30:49
【问题描述】:

所以我正在尝试打包数据包标头,一切正常,除了我需要打包的标头标志,因为字符串只是解包字符串的第一个字符。

例如,

string = "ahhhhhh"
buffer = pack("s", string.encode('UTF-8'))
list = unpack("s", buffer)
print(list)

给我(b'a')

我做错了什么?

【问题讨论】:

    标签: python buffer encode packet struct.pack


    【解决方案1】:

    啊,所以对于我的格式字符串,我必须指定要打包的字符串中的字符数

    string = "ahhhhhh"
    buffer = pack("7s", string.encode('UTF-8'))
    list = unpack("7s", buffer)
    print(list)
    

    应该是正确的

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2022-01-15
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多