10

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]
3
  • 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. Commented Mar 19, 2021 at 3:08
  • To clarify: 0b101101 == 0x2d == 0o55 == 45 (binary, hex, octal, int) results in True. And python always uses integers for __repr__esentation. Commented Mar 19, 2021 at 3:18
  • 1
    Voted to reopen this question. A "duplicate" question asks for string already in hex format to be converted into integer. This is not what this question is about at all. There may be much more elegant answers to this question (I have few good approaches in mind), but I cannot post answer, becasue someone smart thinks this is a duplicate. Commented Jul 27, 2023 at 18:24

1 Answer 1

22

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

Sign up to request clarification or add additional context in comments.

7 Comments

but i taking output from the qline edit box. If i enter in put "AA" so how to make it 0xaa?
@Tenish as explained in the linked duplicate question, use int(string_value, 16). Please, don't rush your answers, read, study, and understand.
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ĐàoMinh no, there's no need for that, int() is well capable of understanding the input even without the 0x prefix.
@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".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.