Android Utililty Methods UTF8 Encode

List of utility methods to do UTF8 Encode

Description

The list of methods to do UTF8 Encode are organized into topic(s).

Method

byte[]stringToUtf8Bytes(String string)
Converts a string into its Java-style UTF-8 form.
int len = string.length();
byte[] bytes = new byte[len * 3]; 
int outAt = 0;
for (int i = 0; i < len; i++) {
    char c = string.charAt(i);
    if ((c != 0) && (c < 0x80)) {
        bytes[outAt] = (byte) c;
        outAt++;
...
byte[]utf8encode(String str)
utfencode
try {
    return UTF8_ENCODER.encode(CharBuffer.wrap(str)).array();
} catch (CharacterCodingException ex) {
    throw new RuntimeException(ex);
booleanisValidUTF8(byte[] input)
is Valid UTF
CharsetDecoder cs = Charset.forName("UTF-8").newDecoder();
try {
    cs.decode(ByteBuffer.wrap(input));
    return true;
} catch (CharacterCodingException e) {
    return false;
byte[]string2BytesUTF8(String str)
string Bytes UTF
byte[] bufByte = new byte[str.length() * 3];
int byteLen = char2ByteUTF8(str, 0, str.length(), bufByte, 0,
        bufByte.length, false);
byte[] ret = new byte[byteLen];
System.arraycopy(bufByte, 0, ret, 0, byteLen);
return ret;
byte[]toUtf8Bytes(String data)
to Utf Bytes
try {
    return data == null ? null : data.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new IllegalStateException(e);
StringtoUtf8String(byte[] data)
to Utf String
try {
    return data == null ? null : new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new IllegalStateException(e);
StringstringFromUtf8ByteArray(byte[] data)
string From Utf Byte Array
try {
    return new String(data, "UTF-8");
} catch (Exception e) {
    return null;