Encodes a String to UTF8
518902
public static String encodeUTF8(String c) {
    StringBuilder r = new StringBuilder();
    for (int i = 0; i < c.length(); i++) {
      r.append(encodeUTF8(c.charAt(i)));
    }
    return (r.toString());
  }
1278134
function encode(str) {
  var result = '',
      bytes = utf8Encode(str),
      length = bytes.length,
      i;

  // Convert every three bytes to 4 ascii characters.
  for (i = 0; i < (length - 2); i += 3) {
    result += chrTable[bytes[i] >> 2];
    result += chrTable[((bytes[i] & 0x03) << 4) + (bytes[i + 1] >> 4)];
    result += chrTable[((bytes[i + 1] & 0x0f) << 2) + (bytes[i + 2] >> 6)];
    result += chrTable[bytes[i + 2] & 0x3f];
  }

  // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
  if (length % 3) {
    i = length - (length % 3);
    result += chrTable[bytes[i] >> 2];
    if ((length % 3) === 2) {
      result += chrTable[((bytes[i] & 0x03) << 4) + (bytes[i + 1] >> 4)];
      result += chrTable[(bytes[i + 1] & 0x0f) << 2];
      result += padding;
    } else {
      result += chrTable[(bytes[i] & 0x03) << 4];
      result += padding + padding;
    }
  }

  return result;
}
1051731
function encode2UTF8(charCode){
    if(charCode <= 0x7f){
      return [charCode];
    }else if(charCode <= 0x7ff){
      return [0xc0|(charCode>>6), 0x80|(charCode & 0x3f)];
    }else{
      return [0xe0|(charCode>>12), 0x80|((charCode & 0xfc0)>>6), 0x80|(charCode & 0x3f)];
    }
  }
1242014
public static String encode(final String str) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (final Exception e) {
            LOGGER.log(Level.WARN, "Encodes str [" + str + "] failed", e);

            return str;
        }
    }
954391
public static void encode(CharSequence in, Appendable out) throws IOException{
		encode(in, UTF_8, out);
	}
300771
def smart_encode_str(s):
    """"""
    try:
        utf16 = s.encode('utf_16_be')
    except AttributeError:  # ints and floats
        utf16 = str(s).encode('utf_16_be')
    safe = utf16.replace(b'\x00)', b'\x00\\)').replace(b'\x00(', b'\x00\\(')
    return b''.join((codecs.BOM_UTF16_BE, safe))
770252
def _utf8str(x):
  """"""
  if six.PY3:
    return str(x)
  if isinstance(x, six.binary_type):
    return x
  elif isinstance(x, six.text_type):
    return x.encode('utf-8')
  else:
    return six.binary_type(x)
1028047
function encode (n) {
  var o = ''
  var i = 0
  var i1, i2, i3, e1, e2, e3, e4
  n = utf8Encode(n)
  while (i < n.length) {
    i1 = n.charCodeAt(i++)
    i2 = n.charCodeAt(i++)
    i3 = n.charCodeAt(i++)
    e1 = (i1 >> 2)
    e2 = (((i1 & 3) << 4) | (i2 >> 4))
    e3 = (isNaN(i2) ? 64 : ((i2 & 15) << 2) | (i3 >> 6))
    e4 = (isNaN(i2) || isNaN(i3)) ? 64 : i3 & 63
    o = o + m.charAt(e1) + m.charAt(e2) + m.charAt(e3) + m.charAt(e4)
  }
  return o
}
476638
private static function toUtf8($string) {

        if(static :: getEncoding() === 'utf-8') {
            $result = $string;
        }
        else {
            $result = static :: convert($string, static :: getEncoding(), 'UTF-8');
        }

        if(false === static :: isUtf8($result)) {
            return trigger_error(sprintf('String to be escaped was not valid UTF-8 or could not be converted'), E_USER_ERROR);
        }

        return $result;
    }
55224
def _EncodeString(self, string):
    
    try:
      # Note that encode() will first convert string into a Unicode string
      # if necessary.
      encoded_string = string.encode(
          self._preferred_encoding, errors=self._encode_errors)
    except UnicodeEncodeError:
      if self._encode_errors == 'strict':
        logging.error(
            'Unable to properly write output due to encoding error. '
            'Switching to error tolerant encoding which can result in '
            'non Basic Latin (C0) characters to be replaced with "?" or '
            '"\\ufffd".')
        self._encode_errors = 'replace'

      encoded_string = string.encode(
          self._preferred_encoding, errors=self._encode_errors)

    return encoded_string
1233794
function encode (str) {
  return str.split('').map((s) => {
    if (s.charCodeAt(0) > 127) return s
    return String.fromCharCode(s.charCodeAt(0) + 0x2800)
  }).join(' ')
}
61254
function string2utf8Bytes(s) {
        var utf8 = unescape(encodeURIComponent(s)),
            len = utf8.length,
            bytes = new Array(len);
        for (var i = 0; i < len; i++) {
            bytes[i] = utf8.charCodeAt(i);
        }
        return bytes;
    }
949796
def utf8(value):
    
    if isinstance(value, _UTF8_TYPES):
        return value
    if not isinstance(value, unicode_type):
        raise TypeError(
            "Expected bytes, unicode, or None; got %r" % type(value)
        )
    return value.encode("utf-8")
1335908
def utf8(string):
    
    if isinstance(string, six.text_type):
        return string.encode('utf8')
    elif isinstance(string, six.binary_type):
        return string
    else:
        return six.text_type(string).encode('utf8')
601427
static ByteBuf encodeString(String str) {
        ByteBuf out = Unpooled.buffer(2);
        byte[] raw;
        try {
            raw = str.getBytes("UTF-8");
            //NB every Java platform has got UTF-8 encoding by default, so this 
            //exception are never raised.
        } catch (UnsupportedEncodingException ex) {
            Log.error("", ex);
            return null;
        }
        //Utils.writeWord(out, raw.length);
        out.writeShort(raw.length);
        out.writeBytes(raw);
        return out;
    }
206646
def encode_string(v, encoding="utf-8"):
    """"""
    if isinstance(encoding, basestring):
        encoding = ((encoding,),) + (("windows-1252",), ("utf-8", "ignore"))
    if isinstance(v, unicode):
        for e in encoding:
            try:
                return v.encode(*e)
            except:
                pass
        return v
    return str(v)
749147
function __decode_utf8(buffer) {

        var text = "";

        var bytes = new Uint8Array(buffer);
        for (var i=0; i<bytes.length; i++) {

            // Get current byte
            var value = bytes[i];

            // Start new codepoint if nothing yet read
            if (bytes_remaining === 0) {

                // 1 byte (0xxxxxxx)
                if ((value | 0x7F) === 0x7F)
                    text += String.fromCharCode(value);

                // 2 byte (110xxxxx)
                else if ((value | 0x1F) === 0xDF) {
                    codepoint = value & 0x1F;
                    bytes_remaining = 1;
                }

                // 3 byte (1110xxxx)
                else if ((value | 0x0F )=== 0xEF) {
                    codepoint = value & 0x0F;
                    bytes_remaining = 2;
                }

                // 4 byte (11110xxx)
                else if ((value | 0x07) === 0xF7) {
                    codepoint = value & 0x07;
                    bytes_remaining = 3;
                }

                // Invalid byte
                else
                    text += "\uFFFD";

            }

            // Continue existing codepoint (10xxxxxx)
            else if ((value | 0x3F) === 0xBF) {

                codepoint = (codepoint << 6) | (value & 0x3F);
                bytes_remaining--;

                // Write codepoint if finished
                if (bytes_remaining === 0)
                    text += String.fromCharCode(codepoint);

            }

            // Invalid byte
            else {
                bytes_remaining = 0;
                text += "\uFFFD";
            }

        }

        return text;

    }