pythonhexasciipyserial

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


I have a list of ASCII "bytes", that I need to convert to hex, and then send over a serial port.

For example, take the following list:

list_to_send=['FE','FE','98','E0''07', 'D2', '00','FD"]

I'd like to convert each byte to hex so that for example the first byte would look like this:

b'\xfe'

I've tried using binascii, but I think my usage is incorrect.


Solution

  • using join()

    str="b'"
    for x in list_to_send:
        str=str+"\x"+x
    str=str+"'"
    

    Sending hex over serial with python