Java UTF8 Convert To getBytesUtf8(String str)

Here you can find the source of getBytesUtf8(String str)

Description

get Bytes Utf

License

Open Source License

Declaration

public static final byte[] getBytesUtf8(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

public class Main {
    private static boolean has_utf8 = false;

    public static final byte[] getBytesUtf8(String str) {
        if (has_utf8) {
            try {
                return str.getBytes("UTF-8");
            } catch (UnsupportedEncodingException usx) {
                // shouldnt...
            }//from   w w  w. j a va  2 s. c om
        }
        char[] chars = str.toCharArray();
        int vlen = chars.length;
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (ch >= 0 && ch <= 0x07F) {
                ;
            } else if (ch >= 0x080 && ch <= 0x07FF) {
                vlen++;
            } else if ((ch >= 0x0800 && ch <= 0x0D7FF) || (ch >= 0x00E000 && ch <= 0x00FFFD)) {
                vlen += 2;
            }
            if (ch >= 0x010000 && ch <= 0x10FFFF) {
                vlen += 3;
            } else {
                /* invalid char, ignore */
                vlen--;
            }
        }
        byte[] buf = new byte[vlen];
        int j = 0;
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (ch >= 0 && ch <= 0x07F) {
                buf[j++] = (byte) (ch & 0x07F);
            } else if (ch >= 0x080 && ch <= 0x07FF) {
                buf[j++] = (byte) (0xC0 | ((ch & 0x07C0) >> 6));
                buf[j++] = (byte) (0x80 | ((ch & 0x003F)));
            } else if ((ch >= 0x0800 && ch <= 0x0D7FF) || (ch >= 0x00E000 && ch <= 0x00FFFD)) {
                buf[j++] = (byte) (0xE0 | ((ch & 0x0F000) >> 12));
                buf[j++] = (byte) (0x80 | ((ch & 0x00FC0) >> 6));
                buf[j++] = (byte) (0x80 | ((ch & 0x0003F)));
            }
            if (ch >= 0x010000 && ch <= 0x10FFFF) {
                /* non dovrebbero essere usate */
                buf[j++] = (byte) (0xE0 | ((ch & 0x1C0000) >> 18));
                buf[j++] = (byte) (0x80 | ((ch & 0x03F000) >> 12));
                buf[j++] = (byte) (0x80 | ((ch & 0x000FC0) >> 6));
                buf[j++] = (byte) (0x80 | ((ch & 0x00003F)));
            }
        }
        return buf;
    }
}

Related

  1. fromUTF8(byte[] bytes)
  2. fromUTF8(byte[] bytes)
  3. fromUTF8(byte[] bytes)
  4. fromUTF8Bytes(byte[] bytes)
  5. fromUTF8Bytes(byte[] bytes)
  6. getBytesUtf8(String string)
  7. getBytesUtf8(String string)
  8. getBytesUTF8(String string)
  9. getBytesUtf8(String string)