Xor of string in ruby

独自空忆成欢 提交于 2019-12-17 17:28:08

问题


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?


回答1:


Found it...

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



回答2:


In Ruby 1.8.7 or 1.9.1:

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


来源:https://stackoverflow.com/questions/348991/xor-of-string-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!