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:com.gistlabs.mechanize.util.apache.URLEncodedUtils.java

/**
 * Emcode/escape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 * //from ww w.  jav a 2s .co  m
 * @param content the portion to decode
 * @param charset the charset to use
 * @param blankAsPlus if {@code true}, then convert space to '+' (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return
 */
private static String urlencode(final String content, final Charset charset, final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null)
        return null;
    StringBuilder buf = new StringBuilder();
    ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        int b = bb.get() & 0xff;
        if (safechars.get(b))
            buf.append((char) b);
        else if (blankAsPlus && b == ' ')
            buf.append('+');
        else {
            buf.append("%");
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}

From source file:jatran.stub.StringUtil.java

/**
 * Returns a string that consists of a random consequence of digits and
 * latin letters in upper case.//from  w  ww . j  av  a 2 s.co  m
 *
 * @param len length of returned token
 * @return random token
 */
public static String getRandomToken(int len) {
    StringBuffer result = new StringBuffer(len);
    for (int i = 0; i < len; i++) {
        result.append(Character.forDigit(RandomUtils.nextInt(36), 36));
    }
    return result.toString().toUpperCase();
}

From source file:com.liveneo.plat.utils.StringUtil.java

/**
 * Returns a string that consists of a random consequence of digits and
 * latin letters in upper case./*  w  w  w . j a v a  2 s .  c  om*/
 * 
 * @param len
 *            length of returned token
 * @return random token
 */
public static String getRandomToken(int len) {
    StringBuffer result = new StringBuffer(len);
    for (int i = 0; i < len; i++) {
        result.append(Character.forDigit(Integer.parseInt(RandomStringUtils.random(36)), 36));
    }
    return result.toString().toUpperCase();
}

From source file:org.jahia.services.content.JCRContentUtils.java

public static String escapeLocalNodeName(String name) {
    name = name.trim();/*from   ww w  .  j a va  2 s.  c  o  m*/
    StringBuilder buffer = new StringBuilder(name.length() * 2);
    for (int i = 0; i < name.length(); i++) {
        char ch = name.charAt(i);
        if (ch == '[' || ch == ']' || ch == '*' || ch == '|') {
            buffer.append('%');
            buffer.append(Character.toUpperCase(Character.forDigit(ch / 16, 16)));
            buffer.append(Character.toUpperCase(Character.forDigit(ch % 16, 16)));
        } else if (ch == '/' || ch == '\\' || Character.isWhitespace(ch)) {
            if (buffer.length() > 0) {
                buffer.append(' ');
            }
        } else {
            buffer.append(ch);
        }
    }
    return buffer.toString();
}

From source file:com.rapleaf.api.personalization.RapleafApi.java

protected String toHex(byte[] a) {
    StringBuilder sb = new StringBuilder(a.length * 2);
    for (int i = 0; i < a.length; i++) {
        sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
        sb.append(Character.forDigit(a[i] & 0x0f, 16));
    }//from  w  w w  . j  a  v  a  2 s .  c om
    return sb.toString();
}

From source file:org.LexGrid.util.sql.DBUtility.java

/**
 * Construct the next identifier to use after the given identifier. Expects
 * (and returns) a 2 character string - using the characters a-z in the
 * first position and the characters a-z and 0-9 in the second position.
 * Case insensitive. Wraps if it gets to zz. Starts with a0 (if no
 * identifier is provided)/*from   w w w. j  av a2s .c  o  m*/
 * 
 * @param currentIdentifier
 * @return
 * @throws Exception
 *             if it doesn't understand the identifier.
 */
public static String computeNextIdentifier(String currentIdentifier) throws Exception {
    String temp;
    if (currentIdentifier == null || currentIdentifier.length() == 0) {
        temp = "a0";
    } else if (StringUtils.isNumeric(currentIdentifier)) {
        temp = "a0";
    } else if (currentIdentifier.length() != 2) {
        throw new Exception("Invalid identifer passed in.  Must be a 2 character string.");
    } else {
        temp = currentIdentifier;
    }

    char a = temp.toLowerCase().charAt(0);
    char b = temp.toLowerCase().charAt(1);

    int ai = Character.getNumericValue(a);
    int bi = Character.getNumericValue(b);

    if (ai < 10 || ai > 35) {
        throw new Exception("Invalid identifer passed in.  First character must be a letter.");
    }

    if (bi < 0 || bi > 35) {
        throw new Exception("Invalid identifer passed in.  Second character must be a letter or a number.");
    }

    if (bi < 35) {
        bi++;
    } else {
        bi = 0;
        ai++;
    }

    if (ai > 35) {
        ai = 10;
    }

    return new String(Character.forDigit(ai, 36) + "" + Character.forDigit(bi, 36));
}

From source file:org.jahia.services.content.JCRContentUtils.java

public static String escapeNodePath(String path) {
    StringBuilder buffer = new StringBuilder(path.length() * 2);
    for (int i = 0; i < path.length(); i++) {
        char ch = path.charAt(i);
        if (ch == '[' || ch == ']' || ch == '*' || ch == '|') {
            buffer.append('%');
            buffer.append(Character.toUpperCase(Character.forDigit(ch / 16, 16)));
            buffer.append(Character.toUpperCase(Character.forDigit(ch % 16, 16)));
        } else {/*  ww  w  . j a v  a  2  s .c o m*/
            buffer.append(ch);
        }
    }
    return buffer.toString();
}

From source file:QCodec.java

/**
 * Encodes byte into its quoted-printable representation.
 * /*w ww . j a  v  a2 s  .co m*/
 * @param b
 *                  byte to encode
 * @param buffer
 *                  the buffer to write to
 */
private static final void encodeQuotedPrintable(int b, ByteArrayOutputStream buffer) {
    buffer.write(ESCAPE_CHAR);
    char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
    char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
    buffer.write(hex1);
    buffer.write(hex2);
}

From source file:com.mcxiaoke.next.http.util.URLUtils.java

private static String urlEncode(final String content, final Charset charset, final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null) {
        return null;
    }/*from w  w  w . java 2 s  . co  m*/
    final StringBuilder buf = new StringBuilder();
    final ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        final int b = bb.get() & 0xff;
        if (safechars.get(b)) {
            buf.append((char) b);
        } else if (blankAsPlus && b == ' ') {
            buf.append('+');
        } else {
            buf.append("%");
            final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}

From source file:org.jivesoftware.community.util.StringUtils.java

public static String URLEncode(String original, String charset) throws UnsupportedEncodingException {
    if (original == null)
        return null;
    byte octets[];
    try {/*w  w  w .ja v  a 2  s  .co m*/
        octets = original.getBytes(charset);
    } catch (UnsupportedEncodingException error) {
        throw new UnsupportedEncodingException();
    }
    StringBuffer buf = new StringBuffer(octets.length);
    byte arr$[] = octets;
    int len$ = arr$.length;
    for (int i$ = 0; i$ < len$; i$++) {
        byte octet = arr$[i$];
        char c = (char) octet;
        if (allowed_query.get(c)) {
            buf.append(c);
        } else {
            buf.append('%');
            char hexadecimal = Character.forDigit(octet >> 4 & 0xf, 16);
            buf.append(Character.toUpperCase(hexadecimal));
            hexadecimal = Character.forDigit(octet & 0xf, 16);
            buf.append(Character.toUpperCase(hexadecimal));
        }
    }

    return buf.toString();
}