Gets UTF-8 bytes from the string.

@param s
@return
8142
public static byte[] bytes(String s) {
    try {
      return s.getBytes(ENCODING);
    } catch (UnsupportedEncodingException e) {
      log.error("UnsupportedEncodingException ", e);
      throw new RuntimeException(e);
    }
  }
368456
@Override
	public Utf8 readString(Utf8 old) throws IOException {
		int length = readInt();
		Utf8 result = (old != null ? old : new Utf8());
		result.setByteLength(length);
		
		if (length > 0) {
			in.readFully(result.getBytes(), 0, length);
		}
		
		return result;
	}
337208
public static String decode(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
		}
		return new String(baKeyword);// UTF-16le:Not
	}
1306557
public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len >> 1];
        for (int i = 0, j = 0; j < len; i++) {
            int x = Character.digit(s.charAt(j), 16) << 4;
            j++;
            x = x | Character.digit(s.charAt(j), 16);
            j++;
            data[i] = (byte) (x & 0xFF);
        }
        return data;
    }
249993
public String getStringF() {
		int len = getInt();
		byte[] utf = new byte[len];
		System.arraycopy(buf, bufPosition, utf, 0, utf.length);
		bufPosition += utf.length;
		return new String(utf, 0, len, charsetUTF8);
	}
511755
public static function strlen($s)
	{
		if (! ReflectionTypeHint::isValid()) return false;
		if (is_null($s)) return $s;

		//since PHP-5.3.x mb_strlen() faster then strlen(utf8_decode())
		if (function_exists('mb_strlen')) return mb_strlen($s, 'utf-8');

		/*
          utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.
          It's much faster than iconv_strlen()
          Note: this function does not count bad UTF-8 bytes in the string - these are simply ignored
		*/
		return strlen(utf8_decode($s));

		/*
        #slowly then strlen(utf8_decode())
        if (function_exists('iconv_strlen')) return iconv_strlen($s, 'utf-8');

        #Do not count UTF-8 continuation bytes
        #return strlen(preg_replace('/[\x80-\xBF]/sSX', '', $s));

        #slowly then strlen(utf8_decode())
        preg_match_all('~.~suSX', $str, $m);
        return count($m[0]);

        #slowly then preg_match_all() + count()
        $n = 0;
        for ($i = 0, $len = strlen($s); $i < $len; $i++)
        {
            $c = ord(substr($s, $i, 1));
            if ($c < 0x80) $n++;                 #single-byte (0xxxxxx)
            elseif (($c & 0xC0) == 0xC0) $n++;   #multi-byte starting byte (11xxxxxx)
        }
        return $n;
		*/
	}
25274
public static byte[] string2Bytes(String str) {
    try {
      final int len = str.length();
      // if we can, we will use it to return the bytes
      byte[] rawBytes = new byte[len];
      
      // get all chars of the given string
      char[] charArray = UTF8.getCharArray(len);
      str.getChars(0, len, charArray, 0);
      
      for (int i = 0; i < len; i++) {
        if (charArray[i] > UTF8.MAX_ASCII_CODE) {
          // non-ASCII chars present
          // do expensive conversion
          return str.getBytes(utf8charsetName);
        }
        // copy to output array
        rawBytes[i] = (byte) charArray[i];
      }
      // only ASCII present - return raw bytes
      return rawBytes;
    } catch (UnsupportedEncodingException e) {
      assert false : "UTF8 encoding is not supported ";
    }
    return null;
  }
396941
private static String uncorrect(final String s) {
        if (s == null) {
            return null;
        }
        if (s.indexOf('%') == -1) {
            // Optimization, nothing to uncorrect here
            return s;
        }
        final StringBuilder sbuf = new StringBuilder();
        final int l = s.length();
        int ch;
        int b = 0, sumb = 0;
        boolean applyUTF8dec = false;

        for (int i = 0, more = -1; i < l; i++) {
            // Get next byte b from URL segment s
            final char current = s.charAt(i);
            ch = current;
            switch (ch) {
            case '%':
                if (i + 2 < s.length()) {
                    // Avoid java.lang.StringIndexOutOfBoundsException...
                    ch = s.charAt(++i);
                    final int hb = (Character.isDigit((char) ch) ? ch - '0'
                            : 10 + Character.toLowerCase((char) ch) - 'a') & 0xF;
                    ch = s.charAt(++i);
                    final int lb = (Character.isDigit((char) ch) ? ch - '0'
                            : 10 + Character.toLowerCase((char) ch) - 'a') & 0xF;
                    b = (hb << 4) | lb;
                    applyUTF8dec = true;
                }
                break;
            default:
                b = ch;
                applyUTF8dec = false;
            }
            // Decode byte b as UTF-8, sumb collects incomplete chars
            if (applyUTF8dec) {
                if ((b & 0xc0) == 0x80) {
                    // 10xxxxxx (continuation byte)
                    sumb = (sumb << 6) | (b & 0x3f); // Add 6 bits to sumb
                    if (--more == 0) {
                        sbuf.append((char) sumb); // Add char to sbuf
                    }
                } else if ((b & 0x80) == 0x00) {
                    // 0xxxxxxx (yields 7 bits)
                    sbuf.append((char) b); // Store in sbuf
                } else if ((b & 0xe0) == 0xc0) {
                    // 110xxxxx (yields 5 bits)
                    sumb = b & 0x1f;
                    more = 1; // Expect 1 more byte
                } else if ((b & 0xf0) == 0xe0) {
                    // 1110xxxx (yields 4 bits)
                    sumb = b & 0x0f;
                    more = 2; // Expect 2 more bytes
                } else if ((b & 0xf8) == 0xf0) {
                    // 11110xxx (yields 3 bits)
                    sumb = b & 0x07;
                    more = 3; // Expect 3 more bytes
                } else if ((b & 0xfc) == 0xf8) {
                    // 111110xx (yields 2 bits)
                    sumb = b & 0x03;
                    more = 4; // Expect 4 more bytes
                } else {
                    sumb = b & 0x01;
                    more = 5; // Expect 5 more bytes
                }
            } else {
                sbuf.append(current);
                // Do not expect other continuation.
                more = -1;
            }
            // We don't test if the UTF-8 encoding is well-formed
        }
        return sbuf.toString();
    }
36988
public static String urlEncode(String s) {
        try {
            return URLEncoder.encode(s, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("get a jdk that actually supports utf-8", e);
        }
    }
737496
public static byte[] strToUtf8(final String str) {
    final ByteBuffer buffer = CHARSET_UTF8.encode(str);
    final byte[] bytesArray = new byte[buffer.remaining()];
    buffer.get(bytesArray, 0, bytesArray.length);
    return bytesArray;
  }
857430
private static final String encode(String s) {
    try {
      return URLEncoder.encode(s, UTF_8);
    } catch (UnsupportedEncodingException e) {
      throw new Error("JVM unable to handle UTF-8");
    }
  }
1233392
def utf8(s):
    
    if isinstance(s, mitogen.core.UnicodeType):
        s = s.encode('utf-8')
    return s
451008
def to_bytes(s):
    
    if bytes != str:
        if type(s) == str:
            return s.encode('utf-8')
    return s
860261
static CharSequence decode(final CharSequence s) {
    if (!contains(s, '%')) {
      return s.toString();
    }
    return decode0(s);
  }
701517
private static int utf8Length(String string) {
    int stringLength = string.length();
    int utf8Length = 0;
    for (int i = 0; i < stringLength; i++) {
      int c = string.charAt(i);
      if (c <= 0x007F) {
        utf8Length++;
      } else if (c > 0x07FF) {
        utf8Length += 3;
      } else {
        utf8Length += 2;
      }
    }
    return utf8Length;
  }
331708
public static byte[] fromHexString(String s) {
		if (s.length() % 2 != 0) {
			throw new IllegalArgumentException(s);
		}
		byte[] array = new byte[s.length() / 2];
		for (int i = 0; i < array.length; i++) {
			int b = Integer.parseInt(s.substring(i * 2, i * 2 + 2), SIXTEEN);
			array[i] = (byte) (_0XFF & b);
		}
		return array;
	}
930419
def to_bytes(s, encoding="utf-8"):
    
    if isinstance(s, six.binary_type):
        return s
    else:
        return six.text_type(s).encode(encoding)