Android Hex String Create appendHexJavaScriptRepresentation(int codePoint, Appendable out)

Here you can find the source of appendHexJavaScriptRepresentation(int codePoint, Appendable out)

Description

Returns a javascript representation of the character in a hex escaped format.

License

Apache License

Parameter

Parameter Description
codePoint The codepoint to append.
out The buffer to which the hex representation should be appended.

Declaration

private static void appendHexJavaScriptRepresentation(int codePoint,
        Appendable out) throws IOException 

Method Source Code

//package com.java2s;
/**//  w  w  w.jav a 2  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(int codePoint, Appendable out)
  2. appendHexJavaScriptRepresentation(StringBuilder sb, char c)
  3. bytesToHexString(byte[] bArray)
  4. makeHex(int val)
  5. makeInt(String hex)
  6. appendHexJavaScriptRepresentation(StringBuilder sb, char c)
  7. hexToBytes(CharSequence str)
  8. hexValue(char c)
  9. isHex(char c)