擅长:python、mysql、java
<p>目前我是这样做的,但不知怎么的,我在想象雷蒙德·海廷格的<a href="https://youtu.be/wf-BqAjZb8M?t=1376" rel="nofollow noreferrer">"There must be a better way!"</a></p>
<pre><code>import io
import functools
from typing import ByteString
from typing import Iterable
# Decoders
int_BE = functools.partial(int.from_bytes, byteorder="big")
utf16_BE = functools.partial(bytes.decode, encoding="utf_16_be")
encoded_strings = b"\x00\x05\x00H\x00e\x00l\x00l\x00o\x00\x06\x00W\x00o\x00r\x00l\x00d\x00!"
header_length = 2
def decode_strings(byte_string: ByteString) -> Iterable[str]:
stream = io.BytesIO(byte_string)
while True:
length = int_BE(stream.read(header_length))
if length:
text = utf16_BE(stream.read(length * 2))
yield text
else:
break
stream.close()
if __name__ == "__main__":
for text in decode_strings(encoded_strings):
print(text)
</code></pre>
<p>谢谢你的建议</p>