Home > Software design >  How do I convert a B string to bytes?
How do I convert a B string to bytes?

Time:12-02

bstr = "b'\\xe4\\xb8\\x96\\xe7\\x95\\x8c'"
bbytes = b'\\xe4\\xb8\\x96\\xe7\\x95\\x8c'

I want to convert bstr to bbytes, how can I do?

CodePudding user response:

You can use the ast.literal_eval (documentation here) function to evaluate this string as a python literal.

import ast

bstr = "b'\\xe4\\xb8\\x96\\xe7\\x95\\x8c'"
bbytes = ast.literal_eval(bstr)
print(bbytes)  # Outputs: b'\xe4\xb8\x96\xe7\x95\x8c'

This function should be safe to use on user inputs (unlike eval), though you should probably enforce a length limit to address the warning about crashing the interpreter with long/complex inputs.

Note this will also correctly parse other valid python literals (such as int, list, etc.), so if you want to enforce that you only end up with bytes you should check that, e.g.

if not isinstance(bbytes, bytes):
  raise ValueError("Input must be a bytes string")

Hopefully you can change the input slightly, I changed the input to escape bstr so the special characters aren't evaluated immediately.

If you're taking this string as user input, e.g. from input or from reading a file, this should already be the case.

If you don't have a properly escaped input, you'll get an exception:

>>> bstr = "b'\xe4\xb8\x96\xe7\x95\x8c'"
>>> ast.literal_eval(bstr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/ast.py", line 48, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
SyntaxError: bytes can only contain ASCII literal characters.
  • Related