我有一个ASCII“字节”列表,我需要转换为十六进制,然后通过串行端口发送。例如,以下面的列表为例:
list_to_send=['FE','FE','98','E0''07', 'D2', '00','FD"]
字符串我想将每个字节转换为十六进制,例如第一个字节看起来像这样:
b'\xfe'
型我试过使用binasktop,但我认为我的用法是不正确的。
dffbzjpn1#
使用join()
str="b'"for x in list_to_send: str=str+"\x"+xstr=str+"'"
str="b'"
for x in list_to_send:
str=str+"\x"+x
str=str+"'"
字符串Sending hex over serial with python
vptzau2j2#
如果您使用的是Python 3.5及更高版本,则使用此
b'\xFE'.hex()
字符串如果你想使用binasktop,那么:
import binasciibinascii.hexlify('FE'.encode('utf8'))
import binascii
binascii.hexlify('FE'.encode('utf8'))
型还有很多其他的方法.更多信息http://code.activestate.com/recipes/510399-byte-to-hex-and-hex-to-byte-string-conversion/
2条答案
按热度按时间dffbzjpn1#
使用join()
字符串
Sending hex over serial with python
vptzau2j2#
如果您使用的是Python 3.5及更高版本,则使用此
字符串
如果你想使用binasktop,那么:
型
还有很多其他的方法.更多信息http://code.activestate.com/recipes/510399-byte-to-hex-and-hex-to-byte-string-conversion/