Example usage for java.lang Character forDigit

List of usage examples for java.lang Character forDigit

Introduction

In this page you can find the example usage for java.lang Character forDigit.

Prototype

public static char forDigit(int digit, int radix) 

Source Link

Document

Determines the character representation for a specific digit in the specified radix.

Usage

From source file:Main.java

public static void writeEscapedChar(Writer writer, char c) throws IOException {
    if ((c >= ' ') && (c < 0x7f)) {
        if ((c == '\'') || (c == '\"') || (c == '\\')) {
            writer.write('\\');
        }//from   www  . j a v a 2 s  . c  om
        writer.write(c);
        return;
    } else if (c <= 0x7f) {
        switch (c) {
        case '\n':
            writer.write("\\n");
            return;
        case '\r':
            writer.write("\\r");
            return;
        case '\t':
            writer.write("\\t");
            return;
        }
    }

    writer.write("\\u");
    writer.write(Character.forDigit(c >> 12, 16));
    writer.write(Character.forDigit((c >> 8) & 0x0f, 16));
    writer.write(Character.forDigit((c >> 4) & 0x0f, 16));
    writer.write(Character.forDigit(c & 0x0f, 16));
}

From source file:Main.java

public static String bytesToHexString(byte[] src) {
    StringBuilder stringBuilder = new StringBuilder("0x");
    if (src == null || src.length <= 0) {
        return null;
    }//from w  ww .ja  v a  2  s  .  c om
    char[] buffer = new char[2];
    for (int i = 0; i < src.length; i++) {
        buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
        buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
        stringBuilder.append(buffer);
    }
    return stringBuilder.toString();
}

From source file:Main.java

public static String escapeString(String value) {
    int len = value.length();
    StringBuilder sb = new StringBuilder(len * 3 / 2);

    for (int i = 0; i < len; i++) {
        char c = value.charAt(i);

        if ((c >= ' ') && (c < 0x7f)) {
            if ((c == '\'') || (c == '\"') || (c == '\\')) {
                sb.append('\\');
            }/*  ww  w.ja v a  2  s .  c  o  m*/
            sb.append(c);
            continue;
        } else if (c <= 0x7f) {
            switch (c) {
            case '\n':
                sb.append("\\n");
                continue;
            case '\r':
                sb.append("\\r");
                continue;
            case '\t':
                sb.append("\\t");
                continue;
            }
        }

        sb.append("\\u");
        sb.append(Character.forDigit(c >> 12, 16));
        sb.append(Character.forDigit((c >> 8) & 0x0f, 16));
        sb.append(Character.forDigit((c >> 4) & 0x0f, 16));
        sb.append(Character.forDigit(c & 0x0f, 16));
    }

    return sb.toString();
}

From source file:Main.java

public static void writeEscapedString(Writer writer, String value) throws IOException {
    for (int i = 0; i < value.length(); i++) {
        char c = value.charAt(i);

        if ((c >= ' ') && (c < 0x7f)) {
            if ((c == '\'') || (c == '\"') || (c == '\\')) {
                writer.write('\\');
            }//ww w  .j  a v  a 2s .  co m
            writer.write(c);
            continue;
        } else if (c <= 0x7f) {
            switch (c) {
            case '\n':
                writer.write("\\n");
                continue;
            case '\r':
                writer.write("\\r");
                continue;
            case '\t':
                writer.write("\\t");
                continue;
            }
        }

        writer.write("\\u");
        writer.write(Character.forDigit(c >> 12, 16));
        writer.write(Character.forDigit((c >> 8) & 0x0f, 16));
        writer.write(Character.forDigit((c >> 4) & 0x0f, 16));
        writer.write(Character.forDigit(c & 0x0f, 16));
    }
}

From source file:Main.java

/**
 *
 * @param data A binary array of bytes.//from w w w .  j a v a 2  s.c o  m
 * @return A hex-encoded string with double length.
 */
public static String hexEncode(byte[] data) {
    if (data == null)
        return null;

    StringBuilder sb = new StringBuilder();
    for (byte b : data) {
        // hex encoding same way as java.net.URLEncoder.
        char ch = Character.forDigit((b >> 4) & 0xF, 16);
        // to uppercase
        if (Character.isLetter(ch)) {
            ch -= caseDiff;
        }
        sb.append(ch);
        ch = Character.forDigit(b & 0xF, 16);
        if (Character.isLetter(ch)) {
            ch -= caseDiff;
        }
        sb.append(ch);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * converts a BCD representation of a number to a String
 * @param b - BCD representation/*from   w  ww  .ja  v  a2 s .  co  m*/
 * @param offset - starting offset
 * @param len - BCD field len
 * @param padLeft - was padLeft packed?
 * @return the String representation of the number
 */
public static String bcd2str(byte[] b, int offset, int len, boolean padLeft) {
    StringBuilder d = new StringBuilder(len);
    int start = (((len & 1) == 1) && padLeft) ? 1 : 0;
    for (int i = start; i < len + start; i++) {
        int shift = ((i & 1) == 1 ? 0 : 4);
        char c = Character.forDigit(((b[offset + (i >> 1)] >> shift) & 0x0F), 16);
        if (c == 'd')
            c = '=';
        d.append(Character.toUpperCase(c));
    }
    return d.toString();
}

From source file:HexUtil.java

public static final void bytesToHexAppend(byte[] bs, int off, int length, StringBuilder sb) {
    if (bs.length <= off || bs.length < off + length)
        throw new IllegalArgumentException();
    sb.ensureCapacity(sb.length() + length * 2);
    for (int i = off; i < (off + length); i++) {
        sb.append(Character.forDigit((bs[i] >>> 4) & 0xf, 16));
        sb.append(Character.forDigit(bs[i] & 0xf, 16));
    }/*from ww w .j  a  v  a 2 s.c om*/
}

From source file:org.official.json.Cookie.java

/**
 * Produce a copy of a string in which the characters '+', '%', '=', ';'
 * and control characters are replaced with "%hh". This is a gentle form
 * of URL encoding, attempting to cause as little distortion to the
 * string as possible. The characters '=' and ';' are meta characters in
 * cookies. By convention, they are escaped using the URL-encoding. This is
 * only a convention, not a standard. Often, cookies are expected to have
 * encoded values. We encode '=' and ';' because we must. We encode '%' and
 * '+' because they are meta characters in URL encoding.
 * @param string The source string.//from w w  w .j  a v  a2  s  . c  o m
 * @return       The escaped result.
 */
public static String escape(String string) {
    char c;
    String s = string.trim();
    int length = s.length();
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; i += 1) {
        c = s.charAt(i);
        if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
            sb.append('%');
            sb.append(Character.forDigit((char) ((c >>> 4) & 0x0f), 16));
            sb.append(Character.forDigit((char) (c & 0x0f), 16));
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}

From source file:org.spirit.util.BotListUniqueId.java

private static final String toHexString(byte[] bytes) {

    char[] ret = new char[bytes.length * 2];
    for (int i = 0, j = 0; i < bytes.length; i++) {
        int c = (int) bytes[i];
        if (c < 0) {
            c += 0x100;/*from  www .  j a v  a2  s  . com*/
        }
        ret[j++] = Character.forDigit(c / 0x10, 0x10);
        ret[j++] = Character.forDigit(c % 0x10, 0x10);

    }

    return new String(ret);
}

From source file:com.jaspersoft.jasperserver.jaxrs.client.core.EncryptionUtils.java

private static String byteArrayToHexString(byte[] byteArr) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteArr.length; i++) {
        byte b = byteArr[i];
        int high = (b & 0xF0) >> 4;
        int low = b & 0x0F;
        sb.append(Character.forDigit(high, 16));
        sb.append(Character.forDigit(low, 16));
    }//from  ww w . j  a  v a 2  s .  c  om
    return sb.toString();
}