utf8 string -> utf16 string
437734
function fromUTF8(input){
    var output = '';
    var len = input.length;
    var i = 0;
    var c1;
    var c2;
    var c3;

    while (i < len)
    {
      c1 = input.charCodeAt(i++);
      if (c1 < 128)
        output += chars[c1];
      else
      {
        c2 = input.charCodeAt(i++);
        if (c1 & 32)
        {
          c3 = input.charCodeAt(i++);
          output += String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        }
        else
          output += String.fromCharCode(((c1 & 31) << 6)  | (c2 & 63));
      }
    }
    return output;
  }
492568
def safe_unicode(string):
    ''''''
    if not isinstance(string, basestring):
        string = unicode(string)
    if isinstance(string, unicode):
        string = string.encode('utf8')
    return string
462200
def text_to_utf8 text
      unless text.valid_encoding? and text.encoding == "UTF-8"
        # TODO: had some ruby 1.9 problems with rchardet ... but ideally it or a similar functionality will be reintroduced
        source_encoding = text.valid_encoding? ? text.encoding : "US-ASCII"
        text = text.encode('UTF-8', source_encoding, {:invalid=>:replace, :undef=>:replace, :replace=>""})
        text = text.gsub("\u0000","") # TODO: this cleanup of nil values isn't supposed to be needed...
      end
      text
    end
295021
def to_utf8
            detect_encoding

            utf16 = self.encoding.to_utf16be(self.value)
            utf16.slice!(0, Encoding::UTF16BE::BOM.size)

            utf16.encode("utf-8", "utf-16be")
        end
1195067
def json_unicode_to_utf8(data):
    """"""
    if isinstance(data, unicode):
        return data.encode('utf-8')
    elif isinstance(data, dict):
        newdict = {}
        for key in data:
            newdict[json_unicode_to_utf8(
                key)] = json_unicode_to_utf8(data[key])
        return newdict
    elif isinstance(data, list):
        return [json_unicode_to_utf8(elem) for elem in data]
    else:
        return data
1110644
def _safe_string(self, source, encoding='utf-8'):
        """"""
        if not isinstance(source, str):
            return source.encode(encoding)
        return str(source)
265382
def to_utf8(value):
  
  if isinstance(value, unicode):
    return value.encode('utf-8')

  assert isinstance(value, str)
  return value
994282
private static function utf8_to_unicode( &$str )
    {
        $unicode = array();
        $values = array();
        $looking_for = 1;

        for ($i = 0; $i < strlen( $str ); $i++ ) {
            $this_value = ord( $str[ $i ] );
            if ( $this_value < 128 ) $unicode[] = $this_value;
            else {
                if ( count( $values ) == 0 ) $looking_for = ( $this_value < 224 ) ? 2 : 3;
                $values[] = $this_value;
                if ( count( $values ) == $looking_for ) {
                    $number = ( $looking_for == 3 ) ?
                        ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
                        ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
                    $unicode[] = $number;
                    $values = array();
                    $looking_for = 1;
                }
            }
        }
        return $unicode;
    }
221530
def to_utf8(str_or_unicode):
    
    if not isinstance(str_or_unicode, six.text_type):
        return str_or_unicode.encode("utf-8", "ignore")
    return str(str_or_unicode)
571542
def unescape_utf8(msg):
    '''  '''
    def sub(m):
        text = m.group(0)
        if text[:3] == "&#x": return unichr(int(text[3:-1], 16))
        else:                 return unichr(int(text[2:-1]))
    return re.sub("&#?\w+;", sub, urllib.unquote(msg))
999828
@Override
  public Utf8 readString(Utf8 old) throws IOException {
    ByteBuffer stringBytes = readBytes(null);
    return new Utf8(stringBytes.array());
  }
899837
function _utf8ArrayToStr(data) {
    const extraByteMap = [1, 1, 1, 1, 2, 2, 3, 0];
    var count = data.length;
    var str = "";

    for (var index = 0; index < count;) {
        var ch = data[index++];
        if (ch & 0x80) {
            var extra = extraByteMap[(ch >> 3) & 0x07];
            if (!(ch & 0x40) || !extra || ((index + extra) > count))
                return null;

            ch = ch & (0x3F >> extra);
            for (; extra > 0; extra -= 1) {
                var chx = data[index++];
                if ((chx & 0xC0) != 0x80)
                    return null;

                ch = (ch << 6) | (chx & 0x3F);
            }
        }

        str += String.fromCharCode(ch);
    }

    return str;
}
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;
    }
538384
static function iconv_fallback_utf16_utf8($string) {
		$bom = substr($string, 0, 2);
		if ($bom == "\xFE\xFF") {
			return getid3_lib::iconv_fallback_utf16be_utf8(substr($string, 2));
		} elseif ($bom == "\xFF\xFE") {
			return getid3_lib::iconv_fallback_utf16le_utf8(substr($string, 2));
		}
		return $string;
	}
1027527
def get_unicode(self):
        
        s = mutf8.decode(self.data)
        if len(s) != self.utf16_size:
            raise ValueError("UTF16 Length does not match!")

        # Return a UTF16 String
        return s
336554
private static String bytesToUTF8(final byte[] body) {
    final String encoded = new String(body, Charset.forName("UTF-8"));
    return encoded;
  }
21525
def to_utf8(text):
    
    # return empty/false stuff unaltered
    if not text:
        if isinstance(text, string_types):
            text = ""
        return text

    try:
        # Is it a unicode string, or pure ascii?
        return text.encode("utf8")
    except UnicodeDecodeError:
        try:
            # Is it a utf8 byte string?
            if text.startswith(codecs.BOM_UTF8):
                text = text[len(codecs.BOM_UTF8):]
            return text.decode("utf8").encode("utf8")
        except UnicodeDecodeError:
            # Check BOM
            if text.startswith(codecs.BOM_UTF16_LE):
                encoding = "utf-16le"
                text = text[len(codecs.BOM_UTF16_LE):]
            elif text.startswith(codecs.BOM_UTF16_BE):
                encoding = "utf-16be"
                text = text[len(codecs.BOM_UTF16_BE):]
            else:
                # Assume CP-1252
                encoding = "cp1252"

            try:
                return text.decode(encoding).encode("utf8")
            except UnicodeDecodeError as exc:
                for line in text.splitlines():
                    try:
                        line.decode(encoding).encode("utf8")
                    except UnicodeDecodeError:
                        log.warn("Cannot transcode the following into UTF8 cause of %s: %r" % (exc, line))
                        break
                return text