pythonpython-3.xiooutputbytestream

Python3 interpret user input string as raw bytes (e.g. \x41 == "A")


Example Code from Python3 Command Prompt:

1 | >>> var_abc = "\x41\x42\x43"
2 | >>> print(var_abc)
3 | ABC
4 | >>> print(bytes(var_abc, encoding='ascii'))
5 | b'ABC'

6 | >>> user_input_abc = input('enter user input in bytes: ')
7 | enter user input in bytes: \x41\x42\x43
8 | >>> print(user_input_abc)
9 | \x41\x42\x43
10| >>> print(bytes(user_input_abc, encoding='ascii'))
11| b'\\x41\\x42\\x43'

Solution

  • To interpret a user input string as raw bytes. You can encode the string, then decode those bytes using the "unicode_escape" encoding and then encode that string again to get the bytes object

    user_input_abc = '\\x41\\x42\\x43'
    print(user_input_abc) # \x41\x42\x43
    user_input_escaped = user_input_abc.encode().decode('unicode_escape')
    print(user_input_escaped) # 'ABC'
    user_input_bytes = user_input_escaped.encode()
    print(user_input_bytes) # b'ABC'