Search code examples
python-3.xhexbyteunicode-escapes

Unicode escaping encoding technique gives this as an output. Why?


I have written a python read script which has the following code`

def read_serial():
    while True:
        # prepare for response
        out = ''
        while ser.inWaiting() > 0:
            out += ser.read(1).decode('unicode_escape')

        if out != '':
            response_header = out.encode('unicode_escape').strip()
            print( "Response recieved " ,response_header)
            print((response_header[0],response_header[1],response_header[2])) //line 2

So the bytes array that I'm sending is:

data[] = {0x9c,0x10,0x01,0x05,0x07,0x08};

and on python side..in line 2 I'm receiving this:

Response recieved  b'\\\x9c\\\x10\\\x01\\\x05\\\x07\\\x08'
(92, 120, 57)

Why 92,120,57 what does this mean because for 9C decimal would be 156?

Any suggestions? Or which encoding technique you suggest for bytes?


Solution

  • Just receive the bytes. Something like this since I can't run your code:

    def read_serial():
        while True:
            # prepare for response
            out = b''   # bytes type
            while ser.inWaiting() > 0:
                out += ser.read(1)
    
            if out != b'':
                print("Response received:",out.hex())