将字节格式化为十六进制字符串

huangapple go评论98阅读模式
英文:

Format bytes as hex string

问题

  1. 我有一个RFID读卡器。物理接口与RS-232规范兼容。
  2. 1个起始位、8个数据位、1个停止位、无奇偶校验。以下是我用于运行读卡器并读取UHF RFID标签的代码:
  3. ```python
  4. import serial
  5. ser = serial.Serial(port='COM8', baudrate=115200, timeout=0.01)
  6. ba_antenna_set_1 = b'\xA0\x04\x01\x74\x00\xE7'
  7. ba_antenna_set_2 = b'\xA0\x04\x01\x74\x10\xD7'
  8. ba_inventory = b'\xA0\x04\x01\x89\x01\xD1'
  9. ser.write(ba_antenna_set_1);
  10. ser.write(ba_antenna_set_2);
  11. while True:
  12. ser.write(ba_inventory);
  13. if ser.in_waiting > 0:
  14. data = ser.readline()
  15. print(data)

运行此代码时,我得到以下输出:

b'\xa0\x04\x01t\x10\xd7'
b'\xa0\x13\x01\x89t0\x000\x083\xb2\xdd\xd9\x01@\x00\x00\x00\x00M\xbe'
b'\xa0\n'
b'\x01\x89\x00\x00\x10\x00\x00\x00\x01\xbb'
b'\xa0\x13\x01\x89\x040\x000\x083\xb2\xdd\xd9\x01@\x00\x00\x00\x00N-'

我希望我的输出如下所示,并且需要帮助:

'A0 04 01 74 00 E7'
'A0 04 01 74 10 D7'
'A0 04 01 89 01 D1'
'A0 13 01 89 24 30 00 30 08 33 B2 DD D9 01 40 00 00 00 00 50 0B'
'A0 0A 01 89 00 00 11 00 00 00 01 BA'

  1. <details>
  2. <summary>英文:</summary>
  3. I have a RFID reader. The physical interface is compatible with the RS – 232 specifications.
  4. 1start bit、8 data bits、1 stop bit、no even odd check.. Here is the code i use to run the reader and read the UHF RFID tags

import serial

ser = serial.Serial(port='COM8', baudrate=115200, timeout=0.01)
ba_antenna_set_1 = b'\xA0\x04\x01\x74\x00\xE7'
ba_antenna_set_2 = b'\xA0\x04\x01\x74\x10\xD7'
ba_inventory = b'\xA0\x04\x01\x89\x01\xD1'
ser.write(ba_antenna_set_1);
ser.write(ba_antenna_set_2);
while True:
ser.write(ba_inventory);
if ser.in_waiting > 0:
data = ser.readline()
print(data)

  1. When I run this code, I get an output like this:
  2. b&#39;\xa0\x04\x01t\x10\xd7&#39;
  3. b&#39;\xa0\x13\x01\x89t0\x000\x083\xb2\xdd\xd9\x01@\x00\x00\x00\x00M\xbe&#39;
  4. b&#39;\xa0\n&#39;
  5. b&#39;\x01\x89\x00\x00\x10\x00\x00\x00\x01\xbb&#39;
  6. b&#39;\xa0\x13\x01\x89\x040\x000\x083\xb2\xdd\xd9\x01@\x00\x00\x00\x00N-&#39;
  7. I want my output to be as below and I need help with this
  8. &#39;A0 04 01 74 00 E7&#39;
  9. &#39;A0 04 01 74 10 D7&#39;
  10. &#39;A0 04 01 89 01 D1&#39;
  11. &#39;A0 13 01 89 24 30 00 30 08 33 B2 DD D9 01 40 00 00 00 00 50 0B&#39;
  12. &#39;A0 0A 01 89 00 00 11 00 00 00 01 BA&#39;
  13. </details>
  14. # 答案1
  15. **得分**: 1
  16. `data=ser.readline()` 返回一个 `bytes` 数组,所以你想将其转换为十六进制字符串。有一个内置的方法可以做到这一点,即 `.hex()`,它默认返回一个没有空格的十六进制字符串,但你可以指定一个空格,还可以使用 `.upper()` a-f 字符转换为大写。
  17. ```python
  18. datastring = data.hex(' ').upper()
  19. print(datastring)
英文:

data=ser.readline() returns a bytes array, so you want that converted to a hex string. There is a built in method for that, .hex(), which returns a hex string with no spaces by default, but you can specify a space, and you can use .upper() to convert the a-f characters to upper case.

  1. datastring = data.hex(&#39; &#39;).upper()
  2. print(datastring)

答案2

得分: -1

  1. data = ser.readline().decode('utf-8')
英文:

try this:

  1. data = ser.readline().decode(&#39;utf-8&#39;)

huangapple
  • 本文由 发表于 2023年6月19日 15:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定