i need to conver strings like that "aa28f5c91a24293a3278" to get result in bytes, like that "b'\xaa(\xf5\xc9\x1a$):2x'" How to do that in python? thanks!
Asked
Active
Viewed 306 times
-1
-
try `"aa28f5c91a24293a3278".encode()` – Ghost Ops Sep 24 '21 at 15:16
-
@don'ttalkjustcode i know, it looks the same when it gets printed, sry didn't thought about that – Ghost Ops Sep 24 '21 at 15:18
-
1It helps if you can tell us what that string is. There is a good answer assuming that this is a hex encoded string. If that's true, post that info in your question. – tdelaney Sep 24 '21 at 15:20
1 Answers
2
With bytes.fromhex(my_hex_string)
:
>>> 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.

Brad Solomon
- 38,521
- 31
- 149
- 235