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:com.example.marcieltorres.nfcproject_serverapp.cardreader.LoyaltyCardReader.java

/**
 * Utility class to convert a hexadecimal string to a byte string.
 *
 * <p>Behavior with input strings containing non-hexadecimal characters is undefined.
 *
 * @param s String containing hexadecimal characters to convert
 * @return Byte array generated from input
 *///from www .  j av  a2s. c  om
public static byte[] HexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
    }
    return data;
}

From source file:UrlEncoder.java

/**
 * {@inheritDoc}/*from  ww w .ja v  a  2s  .c o  m*/
 */
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    if (encodedText.length() == 0)
        return encodedText;
    final StringBuilder result = new StringBuilder();
    final CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == ESCAPE_CHARACTER) {
            boolean foundEscapedCharacter = false;
            // Found the first character in a potential escape sequence, so grab the next two characters ...
            char hexChar1 = iter.next();
            char hexChar2 = hexChar1 != CharacterIterator.DONE ? iter.next() : CharacterIterator.DONE;
            if (hexChar2 != CharacterIterator.DONE) {
                // We found two more characters, but ensure they form a valid hexadecimal number ...
                int hexNum1 = Character.digit(hexChar1, 16);
                int hexNum2 = Character.digit(hexChar2, 16);
                if (hexNum1 > -1 && hexNum2 > -1) {
                    foundEscapedCharacter = true;
                    result.append((char) (hexNum1 * 16 + hexNum2));
                }
            }
            if (!foundEscapedCharacter) {
                result.append(c);
                if (hexChar1 != CharacterIterator.DONE)
                    result.append(hexChar1);
                if (hexChar2 != CharacterIterator.DONE)
                    result.append(hexChar2);
            }
        } else {
            result.append(c);
        }
    }
    return result.toString();
}

From source file:IPAddressUtil.java

public static byte[] textToNumericFormatV6(String src) {
    // Shortest valid string is "::", hence at least 2 chars
    if (src.length() < 2) {
        return null;
    }//  w ww.  j  a v a  2 s . co  m

    int colonp;
    char ch;
    boolean saw_xdigit;
    int val;
    char[] srcb = src.toCharArray();
    byte[] dst = new byte[INADDR16SZ];

    int srcb_length = srcb.length;
    int pc = src.indexOf("%");
    if (pc == srcb_length - 1) {
        return null;
    }

    if (pc != -1) {
        srcb_length = pc;
    }

    colonp = -1;
    int i = 0, j = 0;
    /* Leading :: requires some special handling. */
    if (srcb[i] == ':')
        if (srcb[++i] != ':')
            return null;
    int curtok = i;
    saw_xdigit = false;
    val = 0;
    while (i < srcb_length) {
        ch = srcb[i++];
        int chval = Character.digit(ch, 16);
        if (chval != -1) {
            val <<= 4;
            val |= chval;
            if (val > 0xffff)
                return null;
            saw_xdigit = true;
            continue;
        }
        if (ch == ':') {
            curtok = i;
            if (!saw_xdigit) {
                if (colonp != -1)
                    return null;
                colonp = j;
                continue;
            } else if (i == srcb_length) {
                return null;
            }
            if (j + INT16SZ > INADDR16SZ)
                return null;
            dst[j++] = (byte) ((val >> 8) & 0xff);
            dst[j++] = (byte) (val & 0xff);
            saw_xdigit = false;
            val = 0;
            continue;
        }
        if (ch == '.' && ((j + INADDR4SZ) <= INADDR16SZ)) {
            String ia4 = src.substring(curtok, srcb_length);
            /* check this IPv4 address has 3 dots, ie. A.B.C.D */
            int dot_count = 0, index = 0;
            while ((index = ia4.indexOf('.', index)) != -1) {
                dot_count++;
                index++;
            }
            if (dot_count != 3) {
                return null;
            }
            byte[] v4addr = textToNumericFormatV4(ia4);
            if (v4addr == null) {
                return null;
            }
            for (int k = 0; k < INADDR4SZ; k++) {
                dst[j++] = v4addr[k];
            }
            saw_xdigit = false;
            break; /* '\0' was seen by inet_pton4(). */
        }
        return null;
    }
    if (saw_xdigit) {
        if (j + INT16SZ > INADDR16SZ)
            return null;
        dst[j++] = (byte) ((val >> 8) & 0xff);
        dst[j++] = (byte) (val & 0xff);
    }

    if (colonp != -1) {
        int n = j - colonp;

        if (j == INADDR16SZ)
            return null;
        for (i = 1; i <= n; i++) {
            dst[INADDR16SZ - i] = dst[colonp + n - i];
            dst[colonp + n - i] = 0;
        }
        j = INADDR16SZ;
    }
    if (j != INADDR16SZ)
        return null;
    byte[] newdst = convertFromIPv4MappedAddress(dst);
    if (newdst != null) {
        return newdst;
    } else {
        return dst;
    }
}

From source file:com.vinted.ab.security.Hex.java

/**
 * Converts a hexadecimal character to an integer.
 *
 * @param ch/*from   ww  w  .  j  a  v a  2 s  .com*/
 *            A character to convert to an integer digit
 * @param index
 *            The index of the character in the source
 * @return An integer
 * @throws DecoderException
 *             Thrown if ch is an illegal hex character
 */
protected static int toDigit(char ch, int index) throws DecoderException {
    int digit = Character.digit(ch, 16);
    if (digit == -1) {
        throw new DecoderException("Illegal hexadecimal character " + ch + " at index " + index);
    }
    return digit;
}

From source file:com.meterware.httpunit.HttpUnitUtils.java

/**
 * Decodes an array of URL safe 7-bit characters into an array of
 * original bytes. Escaped characters are converted back to their
 * original representation./*from ww  w . j a  v  a 2 s. c om*/
 *
 * This method is copied from the <b>Jakarta Commons Codec</b>;
 * <code>org.apache.commons.codec.net.URLCodec</code> class.
 *
 * @param pArray array of URL safe characters
 * @return array of original bytes
 */
private static final byte[] decodeUrl(byte[] pArray) throws IllegalArgumentException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < pArray.length; i++) {
        int b = pArray[i];
        if (b == '+') {
            buffer.write(' ');
        } else if (b != '%') {
            buffer.write(b);
        } else {
            try {
                int u = Character.digit((char) pArray[++i], 16);
                int l = Character.digit((char) pArray[++i], 16);
                if (u == -1 || l == -1)
                    throw new IllegalArgumentException("Invalid URL encoding");
                buffer.write((char) ((u << 4) + l));
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException("Invalid URL encoding");
            }
        }
    }
    return buffer.toByteArray();
}

From source file:us.fatehi.creditcardnumber.AccountNumber.java

private boolean luhnCheck() {

    final int length = accountNumber.length();
    int sum = 0;/*w  w w . j a  v  a2s .c om*/
    boolean alternate = false;
    for (int i = length - 1; i >= 0; i--) {
        int digit = Character.digit(accountNumber.charAt(i), 10);
        if (alternate) {
            digit = digit * 2;
            digit = digit > 9 ? digit - 9 : digit;
        }
        sum = sum + digit;
        alternate = !alternate;
    }
    final boolean passesLuhnCheck = sum % 10 == 0;
    return passesLuhnCheck;
}

From source file:com.arm.connector.bridge.core.Utils.java

public static byte[] hexStringToByteArray(String str) {
    int len = str.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4)
                + Character.digit(str.charAt(i + 1), 16));
    }//from  w  ww.  j  a  v  a  2s. c o  m
    return data;
}

From source file:DigestUtils.java

/**
 * Converts a hexadecimal character to an integer.
 * /* w w  w  . j  a  va 2 s  .  com*/
 * @param ch
 *          A character to convert to an integer digit
 * @param index
 *          The index of the character in the source
 * @return An integer
 * @throws DecoderException
 *           Thrown if ch is an illegal hex character
 */
protected static int toDigit(char ch, int index) throws RuntimeException {
    int digit = Character.digit(ch, 16);
    if (digit == -1) {
        throw new RuntimeException("Illegal hexadecimal charcter " + ch + " at index " + index);
    }
    return digit;
}

From source file:com.breadwallet.tools.threads.ImportPrivKeyTask.java

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

From source file:org.ldaptive.DnParser.java

/**
 * Decodes the supplied string attribute value. Unescapes escaped characters.
 * If escaped character is a hex value, it is decoded.
 *
 * @param  value  to decode// w  w w .ja v  a 2 s. c  o  m
 *
 * @return  decoded string
 */
protected static String decodeStringValue(final String value) {
    final StringBuilder sb = new StringBuilder();
    int pos = 0;
    final StringBuilder hexValue = new StringBuilder();
    while (pos < value.length()) {
        char c = value.charAt(pos);
        boolean appendHex = false;
        boolean appendValue = false;
        switch (c) {

        case '\\':
            if (pos + 1 < value.length()) {
                c = value.charAt(++pos);
                // if hexadecimal character add to buffer to decode later
                if (Character.digit(c, HEX_RADIX) != -1) {
                    if (pos + 1 < value.length()) {
                        hexValue.append(c).append(value.charAt(++pos));
                        if (pos + 1 == value.length()) {
                            appendHex = true;
                        }
                    } else {
                        throw new IllegalArgumentException("Invalid HEX value: " + c);
                    }
                } else {
                    appendHex = hexValue.length() > 0;
                    appendValue = true;
                }
            }
            break;

        default:
            appendHex = hexValue.length() > 0;
            appendValue = true;
            break;
        }
        if (appendHex) {
            sb.append(LdapUtils.utf8Encode(decodeHexValue(hexValue.toString().toCharArray())));
            hexValue.setLength(0);
        }
        if (appendValue) {
            sb.append(c);
        }

        pos++;
    }
    return sb.toString();
}