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:URLEncoder.java

private static void toHex(StringBuffer buffer, byte[] b) {
    for (int i = 0; i < b.length; i++) {
        buffer.append('%');

        char ch = Character.forDigit((b[i] >> 4) & 0xF, 16);
        if (Character.isLetter(ch)) {
            ch -= 32;//ww  w  .  j  ava2  s  . c o  m
        }
        buffer.append(ch);

        ch = Character.forDigit(b[i] & 0xF, 16);
        if (Character.isLetter(ch)) {
            ch -= 32;
        }
        buffer.append(ch);
    }
}

From source file:org.rapidcontext.util.BinaryUtil.java

/**
 * Encodes a byte array to a string with hexadecimal numbers.
 *
 * @param data           the byte array//from  ww  w.  ja  v  a2s. c  om
 *
 * @return the hexadecimal string with the converted data
 */
public static String encodeHexString(byte[] data) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < data.length; i++) {
        hexString.append(Character.forDigit((data[i] & 0xF0) >> 4, 16));
        hexString.append(Character.forDigit(data[i] & 0x0F, 16));
    }
    return hexString.toString();
}

From source file:FastDateFormat.java

/**
 * Note: breaks functionality of fieldPosition param. Also:
 * there's a bug in SimpleDateFormat with "S" and "SS", use "SSS"
 * instead if you want a msec field./*from   w  ww  . j a v a  2 s. co m*/
 **/
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
    long dt = date.getTime();
    long ds = dt / 1000;
    if (ds != lastSec) {
        sb.setLength(0);
        df.format(date, sb, fp);
        lastSec = ds;
    } else {
        // munge current msec into existing string
        int ms = (int) (dt % 1000);
        int pos = fp.getEndIndex();
        int begin = fp.getBeginIndex();
        if (pos > 0) {
            if (pos > begin)
                sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
            ms /= 10;
            if (pos > begin)
                sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
            ms /= 10;
            if (pos > begin)
                sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
        }
    }
    toAppendTo.append(sb.toString());
    return toAppendTo;
}

From source file:org.agatom.springatom.data.vin.validator.VinNumberValidator.java

protected boolean validate(final String vinNumber) {
    int crc = 0;//from   w  ww  . ja v  a  2  s. c o  m
    for (int i = 0; i < VIN_LENGTH; i++) {
        crc += WEIGHTS[i] * this.resolveCharacterValue(vinNumber.charAt(i));
    }
    final char checksum = (crc %= 11) == 10 ? 'X' : Character.forDigit(crc, 10);
    final boolean valid = this.isChecksumValid(vinNumber, checksum);
    if (valid && LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("VinNumber=%s is valid [checksum=%s]", vinNumber, checksum));
    } else if (!valid) {
        LOGGER.warn(String.format("VinNumber=%s is invalid [checksum=%s]", vinNumber, checksum));
    }
    return valid;
}

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

public boolean validateISBN(String input) {
    // D-DDD-DDDDD-X or DDD-D-DDD-DDDDD-X 
    if (input != null && ((input.length() == 13
            && Pattern.matches("\\A\\d{1}\\-\\d{3}\\-\\d{5}\\-[X\\d]\\z", input))
            || (input.length() == 13 && Pattern.matches("\\A\\d{1}\\-\\d{4}\\-\\d{4}\\-[X\\d]\\z", input))
            || (input.length() == 17//from  w  w  w .j  a  v a 2s.co  m
                    && Pattern.matches("\\A\\d{3}\\-\\d{1}\\-\\d{2}\\-\\d{6}\\-[X\\d]\\z", input))
            || (input.length() == 17
                    && Pattern.matches("\\A\\d{3}\\-\\d{1}\\-\\d{3}\\-\\d{5}\\-[X\\d]\\z", input)))) {
        int tot = 0;
        int remainder = 0;
        char compChkDigit;

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

        switch (input.length()) {
        case 10:
            int[] weightFactor = { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
            for (int i = 0; i <= 8; i++)
                tot = tot + (Character.getNumericValue(input.charAt(i)) * weightFactor[i]);

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

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

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

        case 13:
            int weight = 0;
            for (int i = 0; i <= 11; i++) {
                if (i % 2 == 0)
                    weight = 1;
                else
                    weight = 3;
                tot = tot + (Character.getNumericValue(input.charAt(i)) * weight);
            }

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

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

From source file:BotlistUniqueId.java

private 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 w  w w .j  a v  a2  s  .  c o  m
        }
        ret[j++] = Character.forDigit(c / 0x10, 0x10);
        ret[j++] = Character.forDigit(c % 0x10, 0x10);

    }

    return new String(ret);
}

From source file:org.hyperic.util.security.MD5.java

public String getDigestString(byte[] bytes) {
    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));
    }/*from   w  w w  . j  av  a 2s.co m*/
    return sb.toString();
}

From source file:org.apache.hadoop.hive.ql.udf.UDFConv.java

private void byte2char(int radix, int fromPos) {
    for (int i = fromPos; i < value.length; i++) {
        value[i] = (byte) Character.toUpperCase(Character.forDigit(value[i], radix));
    }//from   www  .j  a v a  2s  .  c  o  m
}

From source file:org.jiemamy.utils.JmStringUtil.java

/**
 * {@link StrBuilder}??{@code i}16?????/*from ww  w . j a  va 2 s .co  m*/
 * 
 * @param buf ?
 * @param i 
 * @throws IllegalArgumentException {@code buf}?{@code null}???
 */
public static void appendHex(StringBuilder buf, byte i) {
    Validate.notNull(buf);
    buf.append(Character.forDigit((i >> 4) & 0x0F, 16));
    buf.append(Character.forDigit(i & 0x0F, 16)); // CHECKSTYLE IGNORE THIS LINE
}

From source file:URLCodec.java

/**
 * Encodes an array of bytes into an array of URL safe 7-bit 
 * characters. Unsafe characters are escaped.
 *
 * @param urlsafe bitset of characters deemed URL safe
 * @param bytes array of bytes to convert to URL safe characters
 * @return array of bytes containing URL safe characters
 *///from w  w  w  . j av a  2s.co m
public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    if (urlsafe == null) {
        urlsafe = WWW_FORM_URL;
    }

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        if (b < 0) {
            b = 256 + b;
        }
        if (urlsafe.get(b)) {
            if (b == ' ') {
                b = '+';
            }
            buffer.write(b);
        } else {
            buffer.write('%');
            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);
        }
    }
    return buffer.toByteArray();
}