code: function(string) {
    var bytes = [];
    var i = 0;
    var j = 0;
    var wc;
  
    while (i < string.length) {
      wc = string.charCodeAt(i++);
      if (wc >= 0xD800 && wc <= 0xDBFF && i < string.length && string.charCodeAt(i) >= 0xDC00 && string.charCodeAt(i) <= 0xDFFF) {
        /* decode UTF-16 */
        wc = 0x10000 + ((wc & 0x3FF) << 10) + (string.charCodeAt(i++) & 0x3FF);
      }
  
      /* emit lead byte */
      if (wc < 0x80) {
        bytes[j++] = wc;
        count = 0;
      } else if (wc < 0x800) {
        bytes[j++] = 0xC0 | (wc >> 6);
        count = 1;
      } else if (wc < 0x10000) {
        bytes[j++] = 0xE0 | (wc >> 12);
        count = 2;
      } else {
        /* SMP: 21-bit Unicode */
        bytes[j++] = 0xF0 | (wc >> 18);
        count = 3;
      }
  
      /* emit trail bytes, if any */
      while (count) {
        bytes[j++] = 0x80 | ((wc >> (6 * --count)) & 0x3F);
      }
    }
  
    return bytes;
  
  }
docs: /*
  Encode string to an array of bytes using utf8 encoding.
  
  Javascript internally stores character data as utf16 (like java).
  String.charCodeAt() does *not* produce unicode points, but simply
  reflects this internal representation. Thus, it is necessary
  to first decode the utf-16 representation before encoding to
  utf-8.