Java Hex String Create appendHex(StringBuffer sbuf, char ch)

Here you can find the source of appendHex(StringBuffer sbuf, char ch)

Description

append Hex

License

Open Source License

Declaration

private static void appendHex(StringBuffer sbuf, char ch) 

Method Source Code

//package com.java2s;
/*/*w w  w.  j a  v a2  s. co  m*/
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */

public class Main {
    private static void appendHex(StringBuffer sbuf, char ch) {
        int firstLiteral = ch / 256;
        int secLiteral = ch % 256;
        if (firstLiteral == 0 && secLiteral < 127) {
            sbuf.append("%");
            sbuf.append(Integer.toHexString(secLiteral).toUpperCase());
            return;
        }
        if (ch <= 0x07ff) {
            // 2 literals unicode
            firstLiteral = 192 + (firstLiteral << 2) + (secLiteral >> 6);
            secLiteral = 128 + (secLiteral & 63);
            sbuf.append("%");
            sbuf.append(Integer.toHexString(firstLiteral).toUpperCase());
            sbuf.append("%");
            sbuf.append(Integer.toHexString(secLiteral).toUpperCase());
        } else {
            // 3 literals unicode
            int thirdLiteral = 128 + (secLiteral & 63);
            secLiteral = 128 + ((firstLiteral % 16) << 2) + (secLiteral >> 6);
            firstLiteral = 224 + (firstLiteral >> 4);
            sbuf.append("%");
            sbuf.append(Integer.toHexString(firstLiteral).toUpperCase());
            sbuf.append("%");
            sbuf.append(Integer.toHexString(secLiteral).toUpperCase());
            sbuf.append("%");
            sbuf.append(Integer.toHexString(thirdLiteral).toUpperCase());
        }
    }
}

Related

  1. appendHex(int color, StringBuffer buffer)
  2. appendHex(StringBuffer buf, int x)
  3. appendHex(StringBuffer buffer, byte b)
  4. appendHex(StringBuffer buffer, int value)
  5. appendHex(StringBuffer sb, byte b)
  6. appendHex(StringBuffer stringbuffer, byte byte0)
  7. appendHex(StringBuilder bld, byte b)
  8. appendHex(StringBuilder buf, int value, int width)
  9. appendHex(StringBuilder buff, int i)