Android Hex String Create appendHexJavaScriptRepresentation(StringBuilder sb, char c)

Here you can find the source of appendHexJavaScriptRepresentation(StringBuilder sb, char c)

Description

Although this is a rather specific method, it is made public because it is also used by the JSCompiler.

License

Apache License

Declaration

public static void appendHexJavaScriptRepresentation(StringBuilder sb,
        char c) 

Method Source Code

//package com.java2s;
/**/*from  w w  w  . j  a  va2  s . c  o  m*/
 * Copyright (c) 2000, 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.
 */

import java.io.IOException;

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

    /**
     * Returns a javascript representation of the character in a hex escaped
     * format.
     *
     * @param codePoint The codepoint to append.
     * @param out The buffer to which the hex representation should be appended.
     */
    private static void appendHexJavaScriptRepresentation(int codePoint,
            Appendable out) throws IOException {
        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);
            appendHexJavaScriptRepresentation(surrogates[0], out);
            appendHexJavaScriptRepresentation(surrogates[1], out);
            return;
        }
        out.append("\\u").append(HEX_CHARS[(codePoint >>> 12) & 0xf])
                .append(HEX_CHARS[(codePoint >>> 8) & 0xf])
                .append(HEX_CHARS[(codePoint >>> 4) & 0xf])
                .append(HEX_CHARS[codePoint & 0xf]);
    }

    /**
     * Although this is a rather specific method, it is made public
     * because it is also used by the JSCompiler.
     *
     * @see #appendHexJavaScriptRepresentation(int, Appendable)
     */
    public static void appendHexJavaScriptRepresentation(StringBuilder sb,
            char c) {
        try {
            appendHexJavaScriptRepresentation(c, sb);
        } catch (IOException ex) {
            // StringBuilder does not throw IOException.
            throw new RuntimeException(ex);
        }
    }
}

Related

  1. appendHexJavaScriptRepresentation(StringBuilder sb, char c)
  2. bytesToHexString(byte[] bArray)
  3. makeHex(int val)
  4. makeInt(String hex)
  5. appendHexJavaScriptRepresentation(int codePoint, Appendable out)
  6. hexToBytes(CharSequence str)
  7. hexValue(char c)
  8. isHex(char c)
  9. getHexString(int[] b)