Java Unicode Escape unicodeEscape(String s)

Here you can find the source of unicodeEscape(String s)

Description

unicode Escape

License

Open Source License

Declaration

public static String unicodeEscape(String s) 

Method Source Code

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

public class Main {
    private static final char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    public static String unicodeEscape(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if ((c >> 7) > 0) {
                sb.append("\\u");
                sb.append(hexChar[(c >> 12) & 0xF]); // append the hex character for the left-most 4-bits
                sb.append(hexChar[(c >> 8) & 0xF]); // hex for the second group of 4-bits from the left
                sb.append(hexChar[(c >> 4) & 0xF]); // hex for the third group
                sb.append(hexChar[c & 0xF]); // hex for the last group, e.g., the right most 4-bits
            } else {
                sb.append(c);/*from w ww .j  a va  2s. c o  m*/
            }
        }
        return sb.toString();
    }
}

Related

  1. unicodeEscape(char c)
  2. unicodeEscape(Character ch)
  3. unicodeEscape(String s)
  4. unicodeEscape(StringBuilder result, int in, int index)
  5. unicodeEscaped(char ch)
  6. unicodeEscaped(Character ch)
  7. unicodeEscaped(Character ch)