Example usage for java.lang Character digit

List of usage examples for java.lang Character digit

Introduction

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

Prototype

public static int digit(int codePoint, int radix) 

Source Link

Document

Returns the numeric value of the specified character (Unicode code point) in the specified radix.

Usage

From source file:PasswordServlet.java

public static byte[] hexToBytes(char[] hex) {
    int length = hex.length / 2;
    byte[] raw = new byte[length];
    for (int i = 0; i < length; i++) {
        int high = Character.digit(hex[i * 2], 16);
        int low = Character.digit(hex[i * 2 + 1], 16);
        int value = (high << 4) | low;
        if (value > 127)
            value -= 256;/*ww  w . j  av  a 2 s  .co m*/
        raw[i] = (byte) value;
    }
    return raw;
}

From source file:net.servicestack.client.Utils.java

public static byte[] fromHex(String hex) {
    int len = hex.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                + Character.digit(hex.charAt(i + 1), 16));
    }//ww  w  .  j  ava2s .co m
    return data;
}

From source file:com.siphyc.utils.Utilities.java

public static boolean isIntegerCheap(String s, int radix) {
    if (s.isEmpty()) {
        return false;
    }/* w  w w.  ja v  a2 s  .c o m*/
    for (int i = 0; i < s.length(); i++) {
        if (i == 0 && s.charAt(i) == '-') {
            if (s.length() == 1) {
                return false;
            } else {
                continue;
            }
        }
        if (Character.digit(s.charAt(i), radix) < 0) {
            return false;
        }
    }
    return true;
}

From source file:com.flazr.Utils.java

public static byte[] fromHex(char[] hex) {
    int length = hex.length / 2;
    byte[] raw = new byte[length];
    for (int i = 0; i < length; i++) {
        int high = Character.digit(hex[i * 2], 16);
        int low = Character.digit(hex[i * 2 + 1], 16);
        int value = (high << 4) | low;
        if (value > 127) {
            value -= 256;/*  ww w  .j a va 2s  .  com*/
        }
        raw[i] = (byte) value;
    }
    return raw;
}

From source file:com.netscape.cmsutil.util.Utils.java

public static String SpecialURLDecode(String s) {
    if (s == null)
        return null;
    ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());

    for (int i = 0; i < s.length(); i++) {
        int c = s.charAt(i);

        if (c == '+') {
            out.write(' ');
        } else if (c == '#') {
            int c1 = Character.digit(s.charAt(++i), 16);
            int c2 = Character.digit(s.charAt(++i), 16);

            out.write((char) (c1 * 16 + c2));
        } else {//from   w w w  .  j  ava 2s . c o m
            out.write(c);
        }
    } // end for
    return out.toString();
}

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

private void char2byte(int radix, int fromPos) {
    for (int i = fromPos; i < value.length; i++) {
        value[i] = (byte) Character.digit(value[i], radix);
    }//from   w ww. j  av  a  2s  .c  om
}

From source file:Main.java

/**
 * Parse an integer located between 2 given offsets in a string
 *
 * @param value      the string to parse
 * @param beginIndex the start index for the integer in the string
 * @param endIndex   the end index for the integer in the string
 * @return the int/*from   ww w .j  a va2  s.  c  o m*/
 * @throws NumberFormatException if the value is not a number
 */
private static int parseInt(String value, int beginIndex, int endIndex) throws NumberFormatException {
    if (beginIndex < 0 || endIndex > value.length() || beginIndex > endIndex) {
        throw new NumberFormatException(value);
    }
    // use same logic as in Integer.parseInt() but less generic we're not supporting negative values
    int i = beginIndex;
    int result = 0;
    int digit;
    if (i < endIndex) {
        digit = Character.digit(value.charAt(i++), 10);
        if (digit < 0) {
            throw new NumberFormatException("Invalid number: " + value);
        }
        result = -digit;
    }
    while (i < endIndex) {
        digit = Character.digit(value.charAt(i++), 10);
        if (digit < 0) {
            throw new NumberFormatException("Invalid number: " + value);
        }
        result *= 10;
        result -= digit;
    }
    return -result;
}

From source file:com.netscape.cmsutil.util.Utils.java

public static byte[] SpecialDecode(String s) {
    if (s == null)
        return null;
    ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());

    for (int i = 0; i < s.length(); i++) {
        int c = s.charAt(i);

        if (c == '+') {
            out.write(' ');
        } else if (c == '#') {
            int c1 = Character.digit(s.charAt(++i), 16);
            int c2 = Character.digit(s.charAt(++i), 16);

            out.write((char) (c1 * 16 + c2));
        } else {//from   w w w .j a va 2s .  co m
            out.write(c);
        }
    } // end for
    return out.toByteArray();
}

From source file:org.hibernate.validator.internal.constraintvalidators.hv.ModCheckBase.java

/**
 * Returns the numeric {@code int} value of a {@code char}
 *
 * @param value the input {@code char} to be parsed
 *
 * @return the numeric {@code int} value represented by the character.
 *
 * @throws NumberFormatException in case character is not a digit
 *//*from   www  . j av  a 2 s .  c  o  m*/
protected int extractDigit(final char value) throws NumberFormatException {
    if (Character.isDigit(value)) {
        return Character.digit(value, DEC_RADIX);
    } else {
        throw LOG.getCharacterIsNotADigitException(value);
    }
}

From source file:com.mirth.connect.util.TcpUtil.java

public static byte[] stringToByteArray(String str) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    if (StringUtils.isNotBlank(str)) {
        String hexString = str.toUpperCase().replaceAll("[^0-9A-F]", "");

        for (String hexByte : ((hexString.length() % 2 > 0 ? "0" : "") + hexString).split("(?<=\\G..)")) {
            bytes.write((byte) ((Character.digit(hexByte.charAt(0), 16) << 4)
                    + Character.digit(hexByte.charAt(1), 16)));
        }/*from   w  ww. j ava 2s . c  o  m*/
    }

    return bytes.toByteArray();
}