Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do the XOR operation between two hexa strings?

Tags:

ruby

xor

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?

like image 938
sat Avatar asked Sep 08 '12 14:09

sat


People also ask

How do you get XOR of two strings?

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.

How do you XOR two strings in python?

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.

What does an XOR do?

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


1 Answers

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.

like image 70
tokland Avatar answered Dec 21 '22 04:12

tokland