2014-11-22 132 views
0

我正在Python上编写代码以从RFID中读取信息,但我该如何只留下十六进制部分?仅显示十六进制字符串

代码:

import serial as s 
ser = s.Serial('COM8', 9600, timeout=10000) 
rfid = ser.read(13).splitlines() 
print(rfid) 

输出:

[b'\x024500F38EC7FF'] 

预期输出:

4500F38EC7FF 

回答

1

如果你只想ASCII字符如果你想每一个字符为整数的值,您可以分割字符串删除前导0×02字符

>>> x = b'\x024500F38EC7FF' 
>>> x[1:] # skip the first character 
'4500F38EC7FF' 

>>> [ord(c) for c in x] 
[2, 52, 53, 48, 48, 70, 51, 56, 69, 67, 55, 70, 70] 

或者如果你想要每个字符的十六进制表示,你可以使用类似于

>>> ' '.join('%02x' % ord(c) for c in x) 
'02 34 35 30 30 46 33 38 45 43 37 46 46' 
0
line = [b'\x024500F38EC7FF'] 
line[0][1:] 

你的意思是这样?

1

因为splitlines()回报你一个数组,这就是你看到print

尽量简单print rfid[0],可能做你想做的。

>>> x = b'\x024500F38EC7FF' 
>>> print(x) 
4500F38EC7FF