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

Stringutf8Encode(String str)
utf Encode
if (!isEmpty(str) && str.getBytes().length != str.length()) {
    try {
        return URLEncoder.encode(str, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(
                "UnsupportedEncodingException occurred. ", e);
return str;
Stringutf8Encode(String str, String defultReturn)
encoded in utf-8, if exception, return defultReturn
if (!isEmpty(str) && str.getBytes().length != str.length()) {
    try {
        return URLEncoder.encode(str, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        return defultReturn;
return str;
...
StringtoUtf8(String str)
to Utf
try {
    return new String(str.getBytes("ISO8859_1"), "UTF-8");
} catch (Exception e) {
    Log.d("StringUtil-toUtf8", e.getMessage());
    return str;
intbytesUtf8(int c)
bytes Utf
if (c < 0x80) {
    return 1;
} else if (c < 0x00800) {
    return 2;
} else if (c < 0x10000) {
    return 3;
} else if (c < 0x200000) {
    return 4;
...
Stringbytes2StringUTF8(byte[] buf)
bytes String UTF
return bytes2StringUTF8(buf, 0, buf.length, true);
Stringbytes2StringUTF8(byte[] buf, int bufOffset, int bufLength, boolean bigEndian)
format the byte[] to String in UTF-8 encode
int len = bytesUTF8len(buf, bufOffset, bufLength);
char[] cbuf = new char[len];
len = bytes2charsUTF8(buf, bufOffset, bufLength, cbuf, bigEndian);
String str = new String(cbuf, 0, len);
cbuf = null;
return str;
intbytes2charsUTF8(byte[] buf, int bufOffset, int bufLength, char[] cbuf, boolean bigEndian)
encode the byte[] to char[] in UTF-8
int cpos = 0, pos = bufOffset;
byte b1, b2;
b1 = 0;
b2 = 0;
while (pos < (bufOffset + bufLength)) {
    if ((buf[pos] & 0x80) == 0x00) {
        b1 = 0;
        b2 = buf[pos];
...
intbytesUTF8len(byte[] buf, int bufOffset, int bufLength)
get the length of the bytes in UTF-8 rules: 0xxxxxxx or 11xxxxxx is the first the byte of the
int len = 0;
for (int i = bufOffset; i < (bufOffset + bufLength); i++) {
    if (((buf[i]) & 0x80) == 0x00 || ((buf[i]) & 0xc0) == 0xc0) {
        len++;
return len;
intchar2ByteUTF8(String input, int inOff, int inEnd, byte[] output, int outOff, int outEnd, boolean getLengthFlag)
char Byte UTF
char inputChar;
int outputSize;
int charOff = inOff;
int byteOff = outOff;
while (charOff < inEnd) {
    inputChar = input.charAt(charOff);
    if (inputChar < 0x80) {
        outputByte[0] = (byte) inputChar;
...
byte[]getStringInUtf8(final String str)
get String In Utf
final int length = str.length();
boolean expanded = false;
byte[] result = new byte[length];
int i = 0;
int out = 0;
char c;
while (i < length) {
    c = str.charAt(i++);
...