Java Char to Unicode charToUnicodeSequence(final StringBuilder buf, final char ch)

Here you can find the source of charToUnicodeSequence(final StringBuilder buf, final char ch)

Description

char To Unicode Sequence

License

Apache License

Declaration

public static void charToUnicodeSequence(final StringBuilder buf, final char ch) 

Method Source Code

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

public class Main {
    private static final char[] HEX_DIGIT = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };
    private static final int MAX_HEX_DIGIT = 0xF;

    public static void charToUnicodeSequence(final StringBuilder buf, final char ch) {
        buf.append("\\u");
        buf.append(toHex((ch >> 12) & MAX_HEX_DIGIT));
        buf.append(toHex((ch >> 8) & MAX_HEX_DIGIT));
        buf.append(toHex((ch >> 4) & MAX_HEX_DIGIT));
        buf.append(toHex(ch & MAX_HEX_DIGIT));
    }//from  www  .  j ava  2s. c  o  m

    public static char toHex(final int nibble) {
        return HEX_DIGIT[(nibble & MAX_HEX_DIGIT)];
    }

    /**
     * @param data      Data to encode.
     * @param separator How to separate the bytes in the output
     * @return The byte array encoded as a string of hex digits separated by given separator.
     */
    public static String toHex(final byte[] data, final String separator) {
        if (data.length == 0) {
            return "";
        }

        final StringBuilder sb = new StringBuilder(data.length * (separator.length() + 2) - separator.length());

        for (int i = 0; i < data.length; i++) {
            if (i > 0) {
                sb.append(separator);
            }
            sb.append(toHex((data[i] & 0xF0) >> 4));
            sb.append(toHex(data[i] & 0x0F));
        }

        return sb.toString();
    }
}

Related

  1. charToUnicode(char c)
  2. charToUnicodeEscape(char c)
  3. charToUnicodeLiteral(int value)
  4. charToUtf8(char ch, byte[] bytes, int start)
  5. toGreek(char c)