title: How to encode string in Oracle?
body: <p>I have problem with encoding data in Oracle database.
  I want to xor string with another string (mask) and then
  encode it with base64.</p>
  
  <p>In Python this looks like:</p>
  
  <pre><code>def encode_str(s, mask):
      xor_mask = mask
      while len(xor_mask) &lt; len(s):
          xor_mask += mask
      chrs2 = []
      for i in range(len(s)):
          chrs2.append(chr(ord(s[i]) ^ ord(xor_mask[i])))
      s2 = ''.join(chrs2)
      return base64.b64encode(s2)
      #return binascii.hexlify(s2).lower()
  </code></pre>
  
  <p>In PL/SQL I got:</p>
  
  <pre><code>create or replace function ht_encode(str in varchar2, mask in varchar2) return varchar2 as
    xor_mask varchar2(2000);
    result_s varchar2(2000);
    i integer;
    xx integer;
    x char(10);
    ch1 char(10);
    ch2 char(10);
    chrx varchar2(10);
  begin
    result_s := '';
    xor_mask := mask;
    while length(xor_mask) &lt; length(str) loop
      xor_mask := xor_mask || mask;
    end loop;
    for i in 1..length(str) loop
      ch1 := substr(str, i, 1);
      ch2 := substr(xor_mask, i, 1);
      xx := BITXOR(ascii(ch1), ascii(ch2));
      x := xx;
      chrx := rawtohex(x);
      --result_s := result_s || ':' || chrx;
      --result_s := result_s || chrx;
      -- HELP ME HERE!
    end loop;
    --return lower(utl_encode.base64_encode(result_s));
    --return result_s || ' | ' || rawtohex(result_s);
    -- HELP ME HERE!
    return result_s;
  end;
  </code></pre>
  
  <p>(bitxor comes from <a href="http://forums.oracle.com/forums/thread.jspa?threadID=496773" rel="nofollow noreferrer">http://forums.oracle.com/forums/thread.jspa?threadID=496773</a> )</p>
  
  <p>I don't know how to create "binary" string and then encode it to hex or preferable
  to base64.</p>