Forum: Ruby XOR two binary strings

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9b419736f944aade48b85ae3244596c6?d=identicon&s=25 Gary Chris (rlzyoner)
on 2009-01-12 20:28
Howdy,

I am having difficulty finding how to XOR 2 binary strings.
Is there a builtin function for this ?
Something like
return string_one ^ string_two

Thanks
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 Jan-Erik R. (Guest)
on 2009-01-12 20:39
(Received via mailing list)
Gary Chris schrieb:
> Howdy,
>
> I am having difficulty finding how to XOR 2 binary strings.
> Is there a builtin function for this ?
> Something like
> return string_one ^ string_two
>
> Thanks
what do you mean by binary Strings?
"011101"?
use
   Integer("0b#{binarystring}")
to convert it to an integer, then use the XOR and later use
   String#to_s(2)
to convert it back again.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-01-12 20:54
(Received via mailing list)
Gary Chris wrote:
> Howdy,
>
> I am having difficulty finding how to XOR 2 binary strings.
> Is there a builtin function for this ?
> Something like
> return string_one ^ string_two
>
> Thanks

All I can think of at the moment:

a = [0b00000001, 0b00001000].pack("C*")
b = [0b10000001, 0b01000000].pack("C*")

c = a.unpack("C*").zip(b.unpack("C*"))
c = c.map {|x,y| x^y}
c = c.pack("C*")
p c
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-13 08:25
(Received via mailing list)
On 12.01.2009 20:52, Joel VanderWerf wrote:
> All I can think of at the moment:
>
> a = [0b00000001, 0b00001000].pack("C*")
> b = [0b10000001, 0b01000000].pack("C*")
>
> c = a.unpack("C*").zip(b.unpack("C*"))
> c = c.map {|x,y| x^y}
> c = c.pack("C*")
> p c

There's also

irb(main):002:0> ("00000001".to_i(2) ^ "0110".to_i(2)).to_s(2)
=> "111"

Kind regards

  robert
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-13 10:21
I'm not sure if "binary string" meant a string of the form "000101...",
or if it meant two strings treated as arrays of bytes.

If the latter, then

irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> s1 = "abc".to_enum(:each_byte)
=> #<Enumerable::Enumerator:0xb7cc85dc>
irb(main):003:0> s2 = "\x02\x04\x06".to_enum(:each_byte)
=> #<Enumerable::Enumerator:0xb7cc02c4>
irb(main):004:0> s1.zip(s2).map{ |x,y| (x^y).chr }.join
=> "cfe"

ruby 1.9 lets you shorten this to

irb(main):001:0> "abc".bytes.zip("\x02\x04\x06".bytes).map { |x,y|
(x^y).chr }.join
=> "cfe"
Cb6bbc826cd7d9238a2fae344958f7ec?d=identicon&s=25 Sandor Szücs (Guest)
on 2009-01-20 18:47
(Received via mailing list)
On 12.01.2009, at 20:27, Gary Chris wrote:

>  am having difficulty finding how to XOR 2 binary strings.
> Is there a builtin function for this ?
> Something like
> return string_one ^ string_two

There is also facets:

irb> require 'facets/string/xor'
irb> "\000\000\001\001" ^ "\000\001\000\001"  # => "\000\001\001\000"


Hth. regards, Sandor
Szücs
This topic is locked and can not be replied to.