How do I convert a B string to bytes?

lgsd123456
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?

Fish

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a string into bytes in python?

How do I convert a string into a string of bytes in python 2.7

How do I convert a string into a vector of bytes in rust?

How do I convert a Vector of bytes (u8) to a string

How do I convert an array of bytes to string with Delphi?

How do I convert a string of escape sequences to bytes?

How do I convert a bytes object to a string in python?

How can I convert bytes to string In Tensorflow

How do I convert lists and dictionaries into bytes?

How do I convert a list of integers into bytes?

How to convert bytes string to bytes

How do I convert a list of ASCII "Bytes" in Python into Hex Bytes

How to convert bytes to string only removing b"" in python 3

How do I convert a 44 bytes long base64 string (public key) to a 32 bytes long UInt8 array?

how can I convert my String (that represents hex values) to bytes?

How can I convert literal escape sequences in a string to the corresponding bytes?

How can I convert string to bytes in Python, like node js

In Perl, how can I convert an array of bytes to a Unicode string?

How can I convert a string of bytes to a byte object

How can i run ASM opcode in a string variable Or convert it to bytes?

How to convert hex string to bytes

How to convert a string of bytes into an int?

How to convert from bytes to string?

How do I know if a function returns string or bytes?

How do I convert this fetch response to a string?

How do I convert a string into HEX representation?

How do I convert a string to a number in PHP?

How do i convert a character array to a string?

How do I convert a String to a BlobColumn in SSIS