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
Answer from Thuấn Đào Minh on Stack Overflow0xaa
I am wanting to take a normal string, "My water bottle is blue" for example, and converting that into the HEX representation of how it would be stored in a txt file. I've tried .encode, binascii.hexlify, and a few other things. Any help would be appreciated.
Converting string to hex in Python 3.5
python - How to convert a hex string to hex number - Stack Overflow
Problem parsing hex formatted as \x48\x45\x4c\x4c\x4f
Need some help with converting hex to bytearray for long strings
Try this:
hex_str = "0xAD4"
hex_int = int(hex_str, 16)
new_int = hex_int + 0x200
print hex(new_int)
If you don't like the 0x in the beginning, replace the last line with
print hex(new_int)[2:]
Use int function with second parameter 16, to convert a hex string to an integer. Finally, use hex function to convert it back to a hexadecimal number.
print hex(int("0xAD4", 16) + int("0x200", 16)) # 0xcd4
Instead you could directly do
print hex(int("0xAD4", 16) + 0x200) # 0xcd4