I need to convert a string with bytes (string view) to byte object in Python.
string = input() # string = '\xff\x00B'
bs = samefunc(string) # typeof(bs) == bytes, len(bs) == 3
print(bs) # b'\xff'
print(bs) # b'\x00'
print(bs) # b'B'
In my app the string input is so large the self parser will be very slow.
You can use
ast.literal_eval, which is more secure if the input is not 100% in your hands):
s = input() # s = '\\xff\\x00B' bs = eval("b'%s'" % s) # typeof(bs) == bytes, len(bs) == 3 print(bs) # 255 print(bs) # 0 print(bs) # 66