Java Unicode unicodeHTMLEscape(final String s)

Here you can find the source of unicodeHTMLEscape(final String s)

Description

Perform Unicode Escape on Specified String.

License

Apache License

Parameter

Parameter Description
s a parameter

Return

String Escaped

Declaration

public static String unicodeHTMLEscape(final String s) 

Method Source Code

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

public class Main {
    /**/*from w  w w .j  a v a  2s.  c o  m*/
     * Hexadecimal Character Array.
     */
    static final char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    /**
    * Perform Unicode Escape on Specified String.
    *
    * @param s
    * @return String Escaped
    */
    public static String unicodeHTMLEscape(final String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            char[] hexChars = new char[4];
            if ((c >> 7) > 0) {
                sb.append("&#"); // Begin encoding of character.
                hexChars[0] = hexChar[(c >> 12) & 0xF]; // append the hex character for the left-most 4-bits
                hexChars[1] = hexChar[(c >> 8) & 0xF]; // hex for the second group of 4-bits from the left
                hexChars[2] = hexChar[(c >> 4) & 0xF]; // hex for the third group
                hexChars[3] = hexChar[c & 0xF]; // hex for the last group, e.g., the right most 4-bits
                String aString = "" + hexChars[0] + "" + hexChars[1] + "" + hexChars[2] + "" + hexChars[3];
                // Make Hex String into decimal Number.
                Integer a = Integer.valueOf(aString, 16);
                sb.append(a);
                sb.append(";");
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

Related

  1. unicodeChar(char a, char b, char c, char d)
  2. unicodeCodepointToString(char cp, boolean shortenIfPossible, String prefix, boolean upperCase)
  3. unicodeConvert(String str)
  4. unicodeCount(String sStr)
  5. unicodeEncode(String s)
  6. unicodePreservingIndex(String str, int index)
  7. unicodePreservingSubstring(String str, int begin, int end)
  8. unicodeToChar(char[] unicode)
  9. unicodeToHTMLUnicodeEntity(final String text)