Search code examples
rubyxor

how to do the XOR operation between two hexa strings?


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?


Solution

  • 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.