I have two hexadecimal strings.I need to do the XOR
operation between them.
My hexa strings Like,
a = "1A6F2D31567C80644A5BEF2D50B986B";
b = "EF737F481FC7CDAE7C8B40837C80644";
How to do the XOR operation between them? Can you give some guideline to do that?
Approach: The idea is to iterate over both the string character by character and if the character mismatched then add “1” as the character in the answer string otherwise add “0” to the answer string to generate the XOR string.
Use the ^ Operator to Perform the Bitwise Exclusive OR of Two Strings in Python. You can use the ^ operator to perform Bitwise XOR strings in Python.
XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).
That would work for any base:
>> (a.to_i(16) ^ b.to_i(16)).to_s(16)
=> "f51c527949bb4dca36d0afae2c39e2f"
But you can use String#hex for hexadecimal strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With