Convert a string to a bytes string (if necessary, encode in utf8)
7656
def utf8_bytes_string(s):
    """"""
    if sys.version_info[0] == 2:
        if isinstance(s, str):
            return s
        else:
            return s.encode('utf8')
    else:
        if isinstance(s, str):
            return bytes(s, encoding='utf8')
        else:
            return s
958943
private static byte[] stringToBytes(String text) {
        byte[] data;
        try {
            data = text.getBytes(Charset.forName(StandardCharsets.UTF_8.name()));
        } catch (UnsupportedCharsetException ex) {
            data = text.getBytes(Charset.defaultCharset());
        }
        return data;
    }
417439
def _encode(self, value):
        ""
        if isinstance(value, bytes):
            return value
        elif isinstance(value, (int, long)):
            value = str(value).encode('utf-8')
        elif isinstance(value, float):
            value = repr(value).encode('utf-8')
        elif not isinstance(value, basestring):
            value = str(value).encode('utf-8')
        else:
            value = value.encode('utf-8', 'strict')
        return value
406771
def utf8(value):
    
    """
    try:
        if isinstance(value, bytes):
            return value.decode('utf-8')
        else:
            return value
    except Exception:
        raise ValueError('Not UTF-8: %r' % value)
348801
def to_bytes(val):
    
    if val is NoResponse:
        return val
    val = val.replace('\\r', '\r').replace('\\n', '\n')
    return val.encode()
434578
def to_string(s, encoding=None, errors='strict'):
    """"""
    encoding = encoding or 'utf-8'
    if isinstance(s, bytes):
        return s.decode(encoding, errors)
    if not is_string(s):
        s = string_type(s)
    return s
694099
function stringToBytes(str) {
  var bytes = new Array();
  var len, c;
  len = str.length;
  for (var i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if (c >= 0x010000 && c <= 0x10FFFF) {
      bytes.push(((c >> 18) & 0x07) | 0xF0);
      bytes.push(((c >> 12) & 0x3F) | 0x80);
      bytes.push(((c >> 6) & 0x3F) | 0x80);
      bytes.push((c & 0x3F) | 0x80);
    } else if (c >= 0x000800 && c <= 0x00FFFF) {
      bytes.push(((c >> 12) & 0x0F) | 0xE0);
      bytes.push(((c >> 6) & 0x3F) | 0x80);
      bytes.push((c & 0x3F) | 0x80);
    } else if (c >= 0x000080 && c <= 0x0007FF) {
      bytes.push(((c >> 6) & 0x1F) | 0xC0);
      bytes.push((c & 0x3F) | 0x80);
    } else {
      bytes.push(c & 0xFF);
    }
  }
  return bytes;

}
334645
def to_string(s, encoding=None, errors='strict'):
    """"""
    if isinstance(s, bytes):
        return s.decode(encoding or 'utf-8', errors)
    elif not isinstance(s, str):
        return str(s)
    else:
        return s
1108928
def to_str(s):
    
    if bytes != str:
        """
        这里的比较在python2中是True，在Python3中是False
        """
        if type(s) == bytes:
            return s.decode('utf-8')
    return s
1259522
def to_str(s):
    
    if isinstance(s, bytes):
        s = s.decode('utf-8')
    elif not isinstance(s, str):
        s = str(s)
    return s
584144
def bytes_to_str(s, encoding='utf-8'):
    """"""
    if six.PY3 and isinstance(s, bytes):
        return s.decode(encoding)
    return s
374770
def encode(cls, s):
        
        b = ByteString(s)
        be = base64.b64encode(b).strip()
        return String(be)
42779
def to_utf8(string):
    
    if isinstance(string, six.text_type):
        return string.encode('utf8')
    if isinstance(string, six.binary_type):
        return string
    return str(string)
955203
function (bytes) {
                var s = '';
                for (var i = 0, l = bytes.length; i < l; i++) {
                    s += String.fromCharCode(bytes[i]);
                }
                return s;
            }
565508
def cast_bytes(s, encoding='utf8', errors='strict'):
    """"""
    if isinstance(s, bytes):
        return s
    elif isinstance(s, str):
        return s.encode(encoding, errors)
    else:
        raise TypeError("Expected unicode or bytes, got %r" % s)
253060
def to_bytes(s, encoding="utf8"):
    """"""
    if PY_VERSION == 2:
        b = bytes(s)
    elif PY_VERSION == 3:
        b = bytes(s, encoding)
    else:
        raise ValueError("Is Python 4 out already?")

    return b
290359
def to_bytes(instance, encoding='utf-8', error='strict'):
    ''''''
    if isinstance(instance, bytes):
        return instance
    elif hasattr(instance, 'encode'):
        return instance.encode(encoding, error)
    elif isinstance(instance, list):
        return list([to_bytes(item, encoding, error) for item in instance])
    elif isinstance(instance, tuple):
        return tuple([to_bytes(item, encoding, error) for item in instance])
    elif isinstance(instance, dict):
        return dict(
            [(to_bytes(key, encoding, error), to_bytes(value, encoding, error))
                for key, value in instance.items()])
    else:
        return instance