I want to convert string 1F to hex 1F in Python, what should I do?

Question:

num="1F"
nm="1"
nm1="2"
hex(num)^hex(nm)^hex(nm1)

I wrote it like the code above, but hex doesn’t work properly.

I want to convert the string to hexadecimal, and I want an xor operation of the converted value.

What should I do?

Asked By: huntersang

||

Answers:

The variable num can be converted to int using int(num, 16). Other variables nm, nm1 are just integers in form of strings. to convert them use int(nm), int(nm1)

num = "1F"
nm = "1"
nm1 = "2"

result = int(num, 16) ^ int(nm) ^ int(nm1)
print(result)

> 28
Answered By: vignesh kanakavalli
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.