wuntee
wuntee

Reputation: 12480

Ruby, XOR random bytes in binary string

I have a string of binary data and want to pick a single character, and ^ it by 0xff. Is there a simple way to do this? For example:

x = "test\223\434t"
r = rand(x.length)
c = x[r].unpack("H*") ^ 0xff # This doesnt work
# Re concat the string

Upvotes: 1

Views: 1168

Answers (1)

Mladen Jablanović
Mladen Jablanović

Reputation: 44080

bytes = x.bytes.to_a
# => [116, 101, 115, 116, 147, 28, 116]
bytes[rand(bytes.length)] ^ 0xff
# => 139 

Upvotes: 3

Related Questions