Steve
Steve

Reputation:

Xor of string in ruby

I have a string, lets say "123|ABC|test|12345|FF" and I want to xor the ascii value of each character and print the result in hex.

What is the simplest way?

Upvotes: 2

Views: 8470

Answers (2)

Bkkbrad
Bkkbrad

Reputation: 3147

In Ruby 1.8.7 or 1.9.1:

input.bytes.inject { |a,b| a ^ b }.to_s(16)

Upvotes: 4

Steve
Steve

Reputation:

Found it...

lrc = 0
input.each_byte do | c |
    lrc ^= c
end
hexVal = lrc.to_s(16)

Upvotes: 4

Related Questions