将解码字节放入DICT更改为原始字节?

发布于 2025-01-28 23:02:52 字数 484 浏览 4 评论 0原文

我试图将一些字节解码为ASCII,但是在解码为ASCII并将其放置为dict时,它会更改为字节格式(?)

b = b'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

print(b.decode('ascii')) # Hello
converted = b.decode('ascii')
print(converted, type(converted))  # Hello <class 'str'>

test_dict = {}
test_dict["converted"] = converted
print(test_dict) # {'converted': 'Helloo\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'} ???

有人知道这里发生了什么吗?

I am trying to decode some bytes to ASCII, but when decoding to ASCII and putting it in a dict, it changes back to the bytes format (?)

b = b'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

print(b.decode('ascii')) # Hello
converted = b.decode('ascii')
print(converted, type(converted))  # Hello <class 'str'>

test_dict = {}
test_dict["converted"] = converted
print(test_dict) # {'converted': 'Helloo\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'} ???

Does anyone know what is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷鸟归林 2025-02-04 23:02:52

如果您想删除 null targine \ x00,使用替换

converted = b.decode('ascii').replace("\x00", "")

使用您的字典代码,您可以得到:

{'converted': 'Hello'}

If you're looking to remove the null character \x00, use replace:

converted = b.decode('ascii').replace("\x00", "")

With your dictionary code, you get:

{'converted': 'Hello'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文