Ad

How To XOR Two Hex Values If One Of Them Was A String?

- 1 answer

below is a snippet of the problem

string = "test"
for index, character in enumerate(string):
     value = hex(ord(character)) ^ 0xababab

This code is returning an error that it cannot XOR a string and an integer. So how can I get python to deal with the hex(ord(character)) as an integer and not a string?

I tried casting it to an int by adding int(hex(ord(character))) but that returns an error of: Invalid literal for int with base10

Ad

Answer

Just use ord, it's int.

for c in  s:
    print(ord(c) ^ 0xababab)

As you can see, these are equivalent:

1 == 0x01 # True
Ad
source: stackoverflow.com
Ad