Java Hex String Create appendHexEscape(StringBuilder out, int codePoint)

Here you can find the source of appendHexEscape(StringBuilder out, int codePoint)

Description

Appends the Unicode hex escape sequence for the given code point (backslash + 'u' + 4 hex digits) to the given StringBuilder.

License

Apache License

Parameter

Parameter Description
out The StringBuilder to append to.
codePoint The Unicode code point whose hex escape sequence to append.

Declaration

public static void appendHexEscape(StringBuilder out, int codePoint) 

Method Source Code

//package com.java2s;
/*/*from  w  w w. j a  v a  2s.c  o m*/
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /** Hex digits for Soy strings (requires upper-case hex digits). */
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    /**
     * Appends the Unicode hex escape sequence for the given code point (backslash + 'u' + 4 hex
     * digits) to the given StringBuilder.
     *
     * Note: May append 2 escape sequences (surrogate pair) in the case of a supplementary character
     * (outside the Unicode BMP).
     *
     * Adapted from StringUtil.appendHexJavaScriptRepresentation().
     *
     * @param out The StringBuilder to append to.
     * @param codePoint The Unicode code point whose hex escape sequence to append.
     */
    public static void appendHexEscape(StringBuilder out, int codePoint) {

        if (Character.isSupplementaryCodePoint(codePoint)) {
            // Handle supplementary unicode values which are not representable in
            // javascript.  We deal with these by escaping them as two 4B sequences
            // so that they will round-trip properly when sent from java to javascript
            // and back.
            char[] surrogates = Character.toChars(codePoint);
            appendHexEscape(out, surrogates[0]);
            appendHexEscape(out, surrogates[1]);

        } else {
            out.append("\\u").append(HEX_DIGITS[(codePoint >>> 12) & 0xF])
                    .append(HEX_DIGITS[(codePoint >>> 8) & 0xF]).append(HEX_DIGITS[(codePoint >>> 4) & 0xF])
                    .append(HEX_DIGITS[codePoint & 0xF]);
        }
    }
}

Related

  1. appendHexByte(byte b, StringBuffer buf)
  2. appendHexByte(final StringBuilder sb, final byte b)
  3. appendHexBytes(StringBuilder builder, byte[] bytes)
  4. appendHexDumpRowPrefix(StringBuilder dump, int row, int rowStartIndex)
  5. appendHexEntity(final StringBuilder out, final char value)
  6. appendHexJavaScriptRepresentation(StringBuilder sb, char c)
  7. appendHexNumber(StringBuffer target, byte b)
  8. appendHexPair(byte b, StringBuffer sb)
  9. appendHexStream(StringBuilder sb, byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)