Python如何将字符串转换为字节?

2024-06-16 08:36:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要转换像“aa28f5c91a24293a3278”这样的字符串以获得字节形式的结果,比如“b'\xaa(\xf5\xc9\x1a$):2x'” 如何在python中实现这一点?谢谢


Tags: 字符串字节形式x1axf5xaaxc9aa28f5c91a24293a3278
1条回答
网友
1楼 · 发布于 2024-06-16 08:36:05

^{}一起:

>>> s = "aa28f5c91a24293a3278"
>>> bytes.fromhex(s)
b'\xaa(\xf5\xc9\x1a$):2x'

This bytes class method returns a bytes object, decoding the given string object. The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.

相关问题 更多 >