-1

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]
eyllanesc
  • 221,139
  • 17
  • 121
  • 189
CripsyBurger
  • 101
  • 9
  • No, the question is string to int. As explained before, python will always "show" you integers, even if you create a list of hex based numbers: `print([0x32, 0x2f])` will output `[50, 47]`. It doesn't matter the base you *think*, the number won't change. – musicamante Mar 19 '21 at 03:08
  • To clarify: `0b101101 == 0x2d == 0o55 == 45` (binary, hex, octal, int) results in `True`. And python always uses *integers* for `__repr__`esentation. – musicamante Mar 19 '21 at 03:18

1 Answers1

3

The hex() function converts a specified integer number into a hexadecimal string representation.

Use int(x, base) with 16 as base to convert the string x to an integer. Call hex(number) with the integer as number to convert it to hexadecimal.

hex_string = "0xAA"

"0x" also required

an_integer = int(hex_string, 16)

hex_value = hex(an_integer)

print(hex_value)

Output

0xaa

musicamante
  • 31,222
  • 5
  • 26
  • 48
  • but i taking output from the qline edit box. If i enter in put "AA" so how to make it 0xaa? – CripsyBurger Mar 19 '21 at 03:09
  • @Tenish as explained in the linked *duplicate* question, use `int(string_value, 16)`. Please, don't rush your answers, read, *study*, and **understand**. – musicamante Mar 19 '21 at 03:14
  • a = self.slave1.text (), self.slave1.text () will have the value "AA" (the data you entered), you just need to add 0x. a = "0x" + self.slave1.text (). – Thuấn Đào Minh Mar 19 '21 at 03:15
  • 1
    @ThuấnĐàoMinh no, there's no need for that, `int()` is well capable of understanding the input even without the `0x` prefix. – musicamante Mar 19 '21 at 03:16
  • @musiccamante, Thank you so much. I have just learned new knowledge. – Thuấn Đào Minh Mar 19 '21 at 03:18
  • 1
    @ThuấnĐàoMinh also, your first sentence is wrong: `hex()` converts a *number* to string (no matter its type or base), not a string to hex: there's no point in "converting to hex" from a programming point of view; an integer is just an integer, the base is just a "human" representation: 15 is just 15 in decimal, 0xf as hex, or `1111` in binary, 3 hands with open fingers, but it's still *"integer of value 15"*. – musicamante Mar 19 '21 at 03:23
  • @musiccamante, i have eddied my answer. Thank you so much for reviewing. – Thuấn Đào Minh Mar 19 '21 at 03:42