Java UTF8 Encode encodeUTF8(String text)

Here you can find the source of encodeUTF8(String text)

Description

encode UTF

License

Open Source License

Declaration

public static byte[] encodeUTF8(String text) 

Method Source Code


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

import java.io.ByteArrayOutputStream;

public class Main {
    public static byte[] encodeUTF8(String text) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c != 0 && c < '\200') {
                baos.write(c);/*w w  w . j a  v a2 s  . c om*/
            } else if (c == 0 || c >= '\200' && c < '\u0800') {
                baos.write((byte) (0xc0 | 0x1f & c >> 6));
                baos.write((byte) (0x80 | 0x3f & c));
            } else {
                baos.write((byte) (0xe0 | 0xf & c >> 12));
                baos.write((byte) (0x80 | 0x3f & c >> 6));
                baos.write((byte) (0x80 | 0x3f & c));
            }
        }

        byte ret[] = baos.toByteArray();
        return ret;
    }
}

Related

  1. encodeUTF8(String str)
  2. encodeUTF8(String str)
  3. encodeUtf8(String string)
  4. encodeUtf8(String target)
  5. encodeUTF8(String text)
  6. encodeUtf8(String url)
  7. getBufferedWriter(final File outFile, final String outEncoding)
  8. getUTF8Bytes(String string)
  9. getUTF8Bytes(String string)