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:org.xwiki.store.internal.FileSystemStoreUtils.java

private static void encode(char c, StringBuilder builder) {
    if (c == ' ') {
        builder.append("+");
    } else {/*  w w  w  .ja v  a2  s .  com*/
        byte[] ba = String.valueOf(c).getBytes(StandardCharsets.UTF_8);

        for (int j = 0; j < ba.length; j++) {
            builder.append('%');

            char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
            // Make it upper case
            ch = Character.toUpperCase(ch);
            builder.append(ch);

            ch = Character.forDigit(ba[j] & 0xF, 16);
            // Make it upper case
            ch = Character.toUpperCase(ch);
            builder.append(ch);
        }
    }
}

From source file:net.sf.keystore_explorer.utilities.io.HexUtil.java

private static String getHexClearLineDump(byte[] bytes, int len) {
    StringBuffer sbHex = new StringBuffer();
    StringBuffer sbClr = new StringBuffer();

    for (int cnt = 0; cnt < len; cnt++) {
        // Convert byte to int
        byte b = bytes[cnt];
        int i = b & 0xFF;

        // First part of byte will be one hex char
        int i1 = (int) Math.floor(i / 16);

        // Second part of byte will be one hex char
        int i2 = i % 16;

        // Get hex characters
        sbHex.append(Character.toUpperCase(Character.forDigit(i1, 16)));
        sbHex.append(Character.toUpperCase(Character.forDigit(i2, 16)));

        if ((cnt + 1) < len) {
            // Divider between hex characters
            sbHex.append(' ');
        }/*  w w w. j ava  2  s  .c om*/

        // Get clear character

        // Character to display if character not defined in Unicode or is a
        // control charcter
        char c = '.';

        // Not a control character and defined in Unicode
        if ((!Character.isISOControl((char) i)) && (Character.isDefined((char) i))) {
            Character clr = new Character((char) i);
            c = clr.charValue();
        }

        sbClr.append(c);
    }

    /*
     * Put both dumps together in one string (hex, clear) with appropriate
     * padding between them (pad to array length)
     */
    StringBuffer strBuff = new StringBuffer();

    strBuff.append(sbHex.toString());

    int i = bytes.length - len;
    for (int cnt = 0; cnt < i; cnt++) {
        strBuff.append("   "); // Each missing byte takes up three spaces
    }

    strBuff.append("   "); // The gap between hex and clear output is three
    // spaces
    strBuff.append(sbClr.toString());

    return strBuff.toString();
}

From source file:org.kuali.ole.select.validation.impl.StandardNumberValidationImpl.java

public boolean validateISSN(String input) {
    // NNNN-NNNX/*from   w  w  w  .j  a  v a2  s  . c  om*/
    if (input != null && (input.length() == 9 && Pattern.matches("\\A\\d{4}\\-\\d{3}[X\\d]\\z", input))) {
        int tot = 0;
        char compChkDigit;
        int[] weightFactor = { 8, 7, 6, 5, 4, 3, 2 };

        input = input.replaceAll("-", "");

        for (int i = 0; i <= 6; i++)
            tot = tot + (Character.getNumericValue(input.charAt(i)) * weightFactor[i]);

        int remainder = (11 - (tot % 11)) % 11;

        if (remainder < 10)
            compChkDigit = Character.forDigit(remainder, 10);
        else
            compChkDigit = 'X';

        if (compChkDigit == input.charAt(7))
            return true;
        else
            return false;
    }
    return false;
}

From source file:UrlEncoder.java

/**
 * {@inheritDoc}//from w w  w.  j  a  v  a  2 s  . c  o  m
 */
public String encode(String text) {
    if (text == null)
        return null;
    if (text.length() == 0)
        return text;
    final BitSet safeChars = isSlashEncoded() ? RFC2396_UNRESERVED_CHARACTERS
            : RFC2396_UNRESERVED_WITH_SLASH_CHARACTERS;
    final StringBuilder result = new StringBuilder();
    final CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (safeChars.get(c)) {
            // Safe character, so just pass through ...
            result.append(c);
        } else {
            // The character is not a safe character, and must be escaped ...
            result.append(ESCAPE_CHARACTER);
            result.append(Character.toLowerCase(Character.forDigit(c / 16, 16)));
            result.append(Character.toLowerCase(Character.forDigit(c % 16, 16)));
        }
    }
    return result.toString();
}

From source file:Main.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  . j av a  2  s .c  o  m
    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:co.cask.cdap.gateway.util.Util.java

/**
 * Convert a byte array into its hex string representation.
 *
 * @param bytes the byte array to convert
 * @return A hex string representing the bytes
 *///from   w w w. j  ava2  s . com
public static String toHex(byte[] bytes) {
    StringBuilder builder = new StringBuilder(bytes.length * 2);
    for (byte b : bytes) {
        try {
            builder.append(Character.forDigit(b >> 4 & 0x0F, 16));
            builder.append(Character.forDigit(b & 0x0F, 16));
        } catch (IllegalArgumentException e) {
            // odd, that should never happen
            e.printStackTrace();
        }
    }
    return builder.toString();
}

From source file:ArraysX.java

/**
 * Returns the hex String representation of a byte array without prefix 0x.
 * The String is formed by making value[0] the leftmost two digits and
 * value[value.length-1] the rightmost two digits.
 *
 * @param array the byte array/*from w  w w  . j a  v  a 2s.co  m*/
 */
public final static String toHexString(byte[] array) {
    StringBuffer sb = new StringBuffer(array.length * 2 + 8);
    char ch;
    for (int i = 0; i < array.length; i++) {
        // byte will be promote to integer first, mask with 0x0f is a must.
        ch = Character.forDigit(array[i] >>> 4 & 0x0f, 16);
        sb.append(ch);
        ch = Character.forDigit(array[i] & 0x0f, 16);
        sb.append(ch);
    }

    return sb.toString();
}

From source file:edu.cornell.med.icb.goby.reads.ColorSpaceConverter.java

/**
 * Converts a sequence into the equivalent sequence in color space.
 *
 * @param input  The sequence to be converted
 * @param output where the converted sequence should be placed
 * @param anyN   if true, converts color space codes larger or equal to 4 to 'N' characters.
 *//*  w ww. j  a va  2  s .c  o  m*/
public static void convert(final CharSequence input, final MutableString output, final boolean anyN) {
    assert output != null : "The output location must not be null";

    output.setLength(0);
    if (input != null) {
        int position = 0;
        final int length = input.length() - 1; // -1 since we enumerate digrams
        if (input.length() > 0) {

            output.setLength(position + 1);
            output.setCharAt(position++, input.charAt(0));
        }
        for (int index = 0; index < length; ++index) {
            final char code = Character.forDigit(getColorCode(input.charAt(index), input.charAt(index + 1)),
                    10);
            output.setLength(position + 1);
            output.setCharAt(position++, (code >= '4') ? (anyN ? 'N' : code) : code);
        }
        output.setLength(position);
    }
}

From source file:ArraysX.java

/**
 * Returns the octal digit String buffer representation of a byte.
 * @param byte the byte/*from  www  .j a  v  a2  s.com*/
 */
private final static StringBuffer appendOctalDigits(StringBuffer sb, byte b) {
    // b will be promote to integer first, mask with 0x07 is a must.
    return sb.append(Character.forDigit(b >>> 6 & 0x07, 8)).append(Character.forDigit(b >>> 3 & 0x07, 8))
            .append(Character.forDigit(b & 0x07, 8));
}

From source file:org.ringside.client.BaseRequestHandler.java

protected CharSequence md5(CharSequence str) {
    try {//from   w  w w. j  ava 2 s  .c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        ByteArrayInputStream bais = new ByteArrayInputStream(str.toString().getBytes());
        byte[] bytes = new byte[1024];
        int len;

        while ((len = bais.read(bytes, 0, bytes.length)) != -1) {
            md.update(bytes, 0, len);
        }

        bytes = md.digest();

        StringBuffer sb = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; i++) {
            int hi = (bytes[i] >> 4) & 0xf;
            int lo = bytes[i] & 0xf;
            sb.append(Character.forDigit(hi, 16));
            sb.append(Character.forDigit(lo, 16));
        }

        return sb.toString();
    } catch (Exception e) {
        LOG.error("Failed to generate MD5", e);
        return "";
    }
}