Android UTF8 Encode bytes2charsUTF8(byte[] buf, int bufOffset, int bufLength, char[] cbuf, boolean bigEndian)

Here you can find the source of bytes2charsUTF8(byte[] buf, int bufOffset, int bufLength, char[] cbuf, boolean bigEndian)

Description

encode the byte[] to char[] in UTF-8

Parameter

Parameter Description
buf a parameter
cbuf a parameter

Return

the cbuf valid length

Declaration

private static int bytes2charsUTF8(byte[] buf, int bufOffset,
        int bufLength, char[] cbuf, boolean bigEndian) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from www  .  j a v a 2  s.  c om*/
     * encode the byte[] to char[] in UTF-8
     * @param buf
     * @param cbuf
     * @return      the cbuf valid length
     */
    private static int bytes2charsUTF8(byte[] buf, int bufOffset,
            int bufLength, char[] cbuf, boolean bigEndian) {
        int cpos = 0, pos = bufOffset;
        byte b1, b2;
        b1 = 0;
        b2 = 0;
        while (pos < (bufOffset + bufLength)) {
            if ((buf[pos] & 0x80) == 0x00) {
                //U-00000000 - U-0000007F:  0xxxxxxx
                b1 = 0;
                b2 = buf[pos];
                pos++;
            } else if ((buf[pos] & 0xe0) == 0xc0) {
                //U-00000080 - U-000007FF:  110xxxxx 10xxxxxx
                if ((buf[pos + 1] & 0x80) == 0x80) {
                    b1 = (byte) (((buf[pos] & 0x1f) >> 2) & 0xff);
                    b2 = (byte) (((buf[pos] & 0x03) << 6) | (buf[pos + 1] & 0x3f) & 0xff);
                    pos += 2;
                } else {
                    /* invalid format, use ? instead 
                     * -- 2006-3-29 13:55:32 */
                    b1 = 0x00;
                    b2 = 0x3f;
                    pos += 1;
                }
            } else if ((buf[pos] & 0xf0) == 0xe0) { //U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx
                if (((buf[pos + 1] & 0x80) == 0x80)
                        && ((buf[pos + 2] & 0x80) == 0x80)) {
                    b1 = (byte) ((((buf[pos] & 0x0f) << 4) | ((buf[pos + 1] & 0x3f) >> 2)) & 0xff);
                    b2 = (byte) (((buf[pos + 1] & 0x03) << 6) | (buf[pos + 2] & 0x3f) & 0xff);
                    pos += 3;
                } else if ((buf[pos + 1] & 0x80) == 0x80) {
                    /* invalid format, use ? instead 
                     * -- 2006-3-29 13:55:32 */
                    b1 = 0x00;
                    b2 = 0x3f;
                    pos += 2;
                } else {
                    /* invalid format, use ? instead 
                     * -- 2006-3-29 13:55:32 */
                    b1 = 0x00;
                    b2 = 0x3f;
                    pos += 1;
                }
            } else {
                b1 = 0;
                b2 = 0;
                pos++;
                continue;
            }
            if (bigEndian) {
                cbuf[cpos] = (char) (((b1 & 0xff) << 8 | (b2 & 0xff)) & 0xffff);
            } else {
                cbuf[cpos] = (char) (((b2 & 0xff) << 8 | (b1 & 0xff)) & 0xffff);
            }
            cpos++;
        }
        return cpos;
    }
}

Related

  1. utf8Encode(String str, String defultReturn)
  2. toUtf8(String str)
  3. bytesUtf8(int c)
  4. bytes2StringUTF8(byte[] buf)
  5. bytes2StringUTF8(byte[] buf, int bufOffset, int bufLength, boolean bigEndian)
  6. bytesUTF8len(byte[] buf, int bufOffset, int bufLength)
  7. char2ByteUTF8(String input, int inOff, int inEnd, byte[] output, int outOff, int outEnd, boolean getLengthFlag)
  8. getStringInUtf8(final String str)
  9. stringToUtf8Bytes(String string)