2021年3月18日星期四

How to convert string to hex in Python?

I using the pyqt5 line edit box as my input boxes. I want to take the input from the input boxes and convert it from string to hex to send to serial capture. For example I did this but I didn't succeed:

a = hex(self.slave1.text())  b = hex(self.function1.text())  c = hex(self.address_msb1.text())  d = hex(self.address_lsb1.text())  e = hex(self.register_msb1.text())  f = hex(self.register_lsb1.text())  g = hex(self.crc_lsb1.text())  h = hex(self.crc_msb1.text())  hexConvert = [a,b,c,d,e,f,g,h]  

Imagine:

a = "01"  b = "03"  c = "00"  d = "0A"  e = "00"  f = "04"  g = "64"  h = "0B"  

And my expected output is

[0x01, 0x03, 0x00, 0x0A, 0x04, 0x64, 0x0B]  
https://stackoverflow.com/questions/66701955/how-to-convert-string-to-hex-in-python March 19, 2021 at 11:01AM

没有评论:

发表评论