8

I saw this code from an answer on PPCG:

echo BeeABBeeoBodBaBdOdPQBBgDQgDdp^"\n\n\t8b\n\n\t\nb&\nb b  \n%%nb%%%\n%\nQ";

I know PHP casts undefined constants into strings, so the equivalent code is:

echo 'BeeABBeeoBodBaBdOdPQBBgDQgDdp' ^ "\n\n\t8b\n\n\t\nb&\nb b  \n%%nb%%%\n%\nQ";

The output of those are:

Holy Hole In A Donut, Batman!

Can someone explain to me how the XOR of those two strings produce that line of output?

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156

2 Answers2

11

According to this official example, using XOR on strings will operate on ASCII values of each respectly character, so in your example:

  • B ^ \n = 66 ^ 10 = 72 = H;
  • e ^ \n = 101 ^ 10 = 111 = o;
  • e ^ \t = 101 ^ 9 = 108 = l;
  • ...

3v4l result

Passerby
  • 9,715
  • 2
  • 33
  • 50
  • Well that was unexpected. I have exactly the same answer, although I was busy looking up why `60 ^ 10 = 72` before I noticed that someone answered the question. – Spencer Wieczorek Dec 13 '15 at 07:18
  • @SpencerWieczorek That's quite normal in SO :) there's always someone faster than you for just a few seconds. I abandoned so many answer for that reason. – Passerby Dec 13 '15 at 07:24
1

This is simply applying an xor on two characters at a time throughout the string. This is done by the conversion to ASCII and then the result is the ascii values which is the difference of value between the two converted items. See example 2 in PHP's documentation. So the result:

"B" ^ "\n" => 66 ^ 10 => 72 => "H"

And so on on through the entire string.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54