XOR two binary strings

Discussion in 'Ruby' started by Gary Chris, Jan 12, 2009.

  1. Gary Chris

    Gary Chris Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Gary Chris, Jan 12, 2009
    #1
    1. Advertising

  2. Gary Chris

    Jan-Erik R. Guest

    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.
     
    Jan-Erik R., Jan 12, 2009
    #2
    1. Advertising

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

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jan 12, 2009
    #3
  4. On 12.01.2009 20:52, Joel VanderWerf wrote:
    > 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


    There's also

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

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
     
    Robert Klemme, Jan 13, 2009
    #4
  5. 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"
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jan 13, 2009
    #5
  6. 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" # =3D> "\000\001\001\000"


    Hth. regards, Sandor Sz=FCcs
    --
     
    Sandor Szücs, Jan 20, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,947
    darshana
    Oct 26, 2008
  2. dataangel

    Why can't I xor strings?

    dataangel, Oct 8, 2004, in forum: Python
    Replies:
    50
    Views:
    1,208
    Andrew Dalke
    Oct 13, 2004
  3. Phil Frost

    Re: Why can't I xor strings?

    Phil Frost, Oct 8, 2004, in forum: Python
    Replies:
    1
    Views:
    339
    Alex Martelli
    Oct 11, 2004
  4. yaipa
    Replies:
    0
    Views:
    1,128
    yaipa
    Feb 16, 2007
  5. David Garamond

    bitwise AND, OR, XOR, NOT for strings?

    David Garamond, Feb 24, 2004, in forum: Ruby
    Replies:
    3
    Views:
    288
    David Garamond
    Feb 25, 2004
Loading...

Share This Page