Java Char to Unicode charToUtf8(char ch, byte[] bytes, int start)

Here you can find the source of charToUtf8(char ch, byte[] bytes, int start)

Description

char To Utf

License

Apache License

Declaration

public static byte charToUtf8(char ch, byte[] bytes, int start) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static byte charToUtf8(char ch, byte[] bytes, int start) {
        int codePoint = (int) ch;
        //        128 = 1 << 7
        if (codePoint <= 0x007F) {
            bytes[start] = (byte) codePoint;
            return 1;
        } else if (codePoint <= 0x07FF) {
            bytes[start++] = (byte) (3 << 6 | codePoint >>> 6);
            bytes[start] = (byte) (128 | (byte) (codePoint & 0x3F));
            return 2;
        } else if (codePoint <= 0xFFFF) {
            bytes[start++] = (byte) (7 << 5 | codePoint >>> 12);
            byte b = (byte) (128 | (byte) ((codePoint >>> 6) & 0x3F));
            bytes[start++] = b;/*  w w w.  j a va2 s. c o m*/
            b = (byte) (128 | (byte) (codePoint & 0x3F));
            bytes[start] = b;
            return 3;
        } else {
            bytes[start++] = (byte) (15 << 4 | codePoint >>> 18);
            byte b = (byte) (128 | (byte) ((codePoint >>> 12) & 0x3F));
            bytes[start++] = b;
            b = (byte) (128 | (byte) ((codePoint >>> 6) & 0x3F));
            bytes[start++] = b;
            b = (byte) (128 | (byte) (codePoint & 0x3F));
            bytes[start] = b;
            return 4;
        }
    }
}

Related

  1. charToUnicode(char c)
  2. charToUnicodeEscape(char c)
  3. charToUnicodeLiteral(int value)
  4. charToUnicodeSequence(final StringBuilder buf, final char ch)
  5. toGreek(char c)