0

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.

Thanks!

2 Answers2

0

if you are using Python 3.5 and above,then use this

b'\xFE'.hex()

And if you wanna use binascii ,then :

import binascii
binascii.hexlify('FE'.encode('utf8'))

there are many other ways too... for more info http://code.activestate.com/recipes/510399-byte-to-hex-and-hex-to-byte-string-conversion/

J Shaikh
  • 36
  • 1
  • 8
0

using join()

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

Sending hex over serial with python

J Shaikh
  • 36
  • 1
  • 8