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.rockagen.commons.util.CommUtil.java

/**
 * Decode hex string//from   w w  w.j a v  a 2  s  .  co  m
 *
 * @param hex string
 * @return decode bytes
 */
public static byte[] hexdecode(String hex) {

    if (hex == null || hex.equals("")) {
        return new byte[0];
    }

    char[] orig = hex.toCharArray();
    int len = orig.length;
    //Adjust odd-length
    if ((len & 1) == 1) {
        len++;
    }
    char[] hexc = new char[len];
    System.arraycopy(orig, 0, hexc, 0, orig.length);

    byte[] bytes = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        bytes[i / 2] = (byte) ((Character.digit(hexc[i], 16) << 4) | Character.digit(hexc[i + 1], 16));
    }
    return bytes;

}

From source file:com.github.mjdetullio.jenkins.plugins.multibranch.AbstractMultiBranchProject.java

/**
 * Inverse function of {@link hudson.Util#rawEncode(String)}.
 * <br>/*from   w  w w. ja va 2s  .  co  m*/
 * Kanged from Branch API.
 *
 * @param s the encoded string.
 * @return the decoded string.
 */
public static String rawDecode(String s) {
    s = s.replace("___", "%2F");

    final byte[] bytes; // should be US-ASCII but we can be tolerant
    try {
        bytes = s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("JLS specification mandates UTF-8 as a supported encoding", e);
    }

    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        final int b = bytes[i];
        if (b == '%' && i + 2 < bytes.length) {
            final int u = Character.digit((char) bytes[++i], 16);
            final int l = Character.digit((char) bytes[++i], 16);

            if (u != -1 && l != -1) {
                buffer.write((char) ((u << 4) + l));
                continue;
            }

            // should be a valid encoding but we can be tolerant
            i -= 2;
        }
        buffer.write(b);
    }

    try {
        return new String(buffer.toByteArray(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("JLS specification mandates UTF-8 as a supported encoding", e);
    }
}

From source file:com.compal.telephonytest.TelephonyTest.java

public boolean checkGsmDeviceId(String deviceId) {
    // IMEI may include the check digit
    String imeiPattern = "[0-9]{14,15}";
    int expectedCheckDigit = getLuhnCheckDigit(deviceId.substring(0, 14));
    int actualCheckDigit = Character.digit(deviceId.charAt(14), 10);
    if (Pattern.matches(imeiPattern, deviceId)) {
        if (deviceId.length() == 15)
            if (expectedCheckDigit == actualCheckDigit)
                return true;
        return true;
    }/* w w  w.j  av a 2  s  .c  om*/
    return false;
}

From source file:com.compal.telephonytest.TelephonyTest.java

/**
* Calculate the check digit by starting from the right, doubling every
* each digit, summing all the digits including the doubled ones, and
* finding a number to make the sum divisible by 10.
*
* @param deviceId not including the check digit
* @return the check digit//from  www .  ja v  a  2s  .co m
*/
private static int getLuhnCheckDigit(String deviceId) {
    int sum = 0;
    int dontDoubleModulus = deviceId.length() % 2;
    for (int i = deviceId.length() - 1; i >= 0; --i) {
        int digit = Character.digit(deviceId.charAt(i), 10);
        if (i % 2 == dontDoubleModulus) {
            sum += digit;
        } else {
            sum += DOUBLE_DIGIT_SUM[digit];
        }
    }
    sum %= 10;
    return sum == 0 ? 0 : 10 - sum;
}

From source file:org.jwebsocket.util.Tools.java

/**
 * From//  www.j av a2s  .  c  o m
 * http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
 *
 *
 * @param aString
 * @return
 */
public static byte[] hexStringToByteArray(String aString) {
    int lLength = aString.length();
    byte[] lData = new byte[lLength / 2];
    for (int lIndex = 0; lIndex < lLength; lIndex += 2) {
        lData[lIndex / 2] = (byte) ((Character.digit(aString.charAt(lIndex), 16) << 4)
                + Character.digit(aString.charAt(lIndex + 1), 16));
    }
    return lData;
}

From source file:org.botlibre.util.Utils.java

public static byte[] hexToBytes(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));
    }/*ww w. ja  va 2  s .  c  o  m*/
    return data;
}

From source file:org.apache.hadoop.hive.serde2.lazy.fast.LazySimpleDeserializeRead.java

private boolean parseLongFast() {

    // Parse without using exceptions for better performance.
    int i = fieldStart;
    int end = fieldStart + fieldLength;
    boolean negative = false;
    if (i >= end) {
        return false; // Empty field.
    }/*  w  ww  .  ja  v  a 2  s.c  o m*/
    if (bytes[i] == '+') {
        i++;
        if (i >= end) {
            return false;
        }
    } else if (bytes[i] == '-') {
        negative = true;
        i++;
        if (i >= end) {
            return false;
        }
    }
    // Skip leading zeros.
    boolean atLeastOneZero = false;
    while (true) {
        if (bytes[i] != '0') {
            break;
        }
        i++;
        if (i >= end) {
            saveLong = 0;
            return true;
        }
        atLeastOneZero = true;
    }
    // We tolerate and ignore decimal places.
    if (bytes[i] == '.') {
        if (!atLeastOneZero) {
            return false;
        }
        saveLong = 0;
        // Fall through below and verify trailing decimal digits.
    } else {
        if (!Character.isDigit(bytes[i])) {
            return false;
        }
        int nonLeadingZeroStart = i;
        int digitCount = 1;
        saveLong = Character.digit(bytes[i], 10);
        i++;
        while (i < end) {
            if (!Character.isDigit(bytes[i])) {
                break;
            }
            digitCount++;
            if (digitCount > maxLongDigitsCount) {
                return false;
            } else if (digitCount == maxLongDigitsCount) {
                // Use the old trick of comparing against number string to check for overflow.
                if (!negative) {
                    if (byteArrayCompareRanges(bytes, nonLeadingZeroStart, maxLongBytes, 0, digitCount) >= 1) {
                        return false;
                    }
                } else {
                    if (byteArrayCompareRanges(bytes, nonLeadingZeroStart, minLongNoSignBytes, 0,
                            digitCount) >= 1) {
                        return false;
                    }
                }
            }
            saveLong = (saveLong * 10) + Character.digit(bytes[i], 10);
        }
        if (negative) {
            // Safe because of our number string comparision against min (negative) long.
            saveLong = -saveLong;
        }
        if (i >= end) {
            return true;
        }
        if (bytes[i] != '.') {
            return false;
        }
    }
    // Fall through to here if we detect the start of trailing decimal digits...
    // We verify trailing digits only.
    while (true) {
        i++;
        if (i >= end) {
            break;
        }
        if (!Character.isDigit(bytes[i])) {
            return false;
        }
    }
    return true;
}

From source file:net.sf.firemox.tools.MToolKit.java

/**
 * Parses the string argument as a signed integer in the radix specified by
 * the second argument. The characters in the string must all be digits of 10
 * radix (as determined by whether//from   w  ww  .j  a  v  a 2 s  .c  o m
 * {@link java.lang.Character#digit(char, int)} returns a nonnegative value),
 * except that the first character may be an ASCII minus sign <code>'-'</code> (<code>'&#92;u002D'</code>)
 * to indicate a negative value. The resulting integer value is returned.
 * <p>
 * The <code>Integer#MIN_VALUE</code> value is returned if any of the
 * following situations occurs:
 * <ul>
 * <li>The first argument is <code>null</code> or is a string of length
 * zero.
 * <li>Any character of the string is not a digit of the specified radix,
 * except that the first character may be a minus sign <code>'-'</code> (<code>'&#92;u002D'</code>)
 * provided that the string is longer than length 1.
 * <li>The value represented by the string is not a value of type
 * <code>int</code>.
 * </ul>
 * <p>
 * Examples: <blockquote> parseInt(&quot;0&quot;) returns 0 <br>
 * parseInt(&quot;473&quot;) returns 473 <br>
 * parseInt(&quot;-0&quot;) returns 0 <br>
 * parseInt(&quot;2147483647&quot;) returns 2147483647 <br>
 * parseInt(&quot;-2147483648&quot;) returns -2147483648 <br>
 * parseInt(&quot;2147483648&quot;) returns Integer#MIN_VALUE <br>
 * parseInt(&quot;Kona&quot;) returns Integer#MIN_VALUE <br>
 * </blockquote>
 * 
 * @param s
 *          the <code>String</code> containing the integer representation to
 *          be parsed
 * @return the integer represented by the string argument in the 10 radix.
 *         Return Integer.MIN_VALUE when error
 * @see Integer#MIN_VALUE
 */
public static int parseInt(String s) {
    if (s == null) {
        return Integer.MIN_VALUE;
    }
    int result = 0;
    boolean negative = false;
    int i = 0;
    int max = s.length();
    int limit;
    int multmin;
    int digit;

    if (max > 0) {
        if (s.charAt(0) == '-') {
            negative = true;
            limit = Integer.MIN_VALUE;
            i++;
        } else {
            limit = -Integer.MAX_VALUE;
        }
        multmin = limit / 10;
        if (i < max) {
            digit = Character.digit(s.charAt(i++), 10);
            if (digit < 0) {
                return Integer.MIN_VALUE;
            }
            result = -digit;
        }
        while (i < max) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++), 10);
            if (digit < 0) {
                return Integer.MIN_VALUE;
            }
            if (result < multmin) {
                return Integer.MIN_VALUE;
            }
            result *= 10;
            if (result < limit + digit) {
                return Integer.MIN_VALUE;
            }
            result -= digit;
        }
    } else {
        return Integer.MIN_VALUE;
    }
    if (negative) {
        if (i > 1) {
            return result;
        }
        /* Only got "-" */
        return Integer.MIN_VALUE;
    }
    return -result;
}

From source file:com.welfare.common.util.UtilValidate.java

public static int getLuhnSum(String stPassed) {
    stPassed = stPassed.replaceAll("\\D", ""); // nuke any non-digit characters

    int len = stPassed.length();
    int sum = 0;//from   ww w  .  j  a v a 2 s. c  o  m
    int mul = 1;
    for (int i = len - 1; i >= 0; i--) {
        int digit = Character.digit(stPassed.charAt(i), 10);
        digit *= (mul == 1) ? mul++ : mul--;
        sum += (digit >= 10) ? (digit % 10) + 1 : digit;
    }

    return sum;
}

From source file:de.syss.MifareClassicTool.Common.java

/**
 * Convert a string of hex data into a byte array.
 * Original author is: Dave L. (http://stackoverflow.com/a/140861).
 * @param s The hex string to convert/*  w  w w . ja va2 s . com*/
 * @return An array of bytes with the values of the string.
 */
public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    try {
        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));
        }
    } catch (Exception e) {
        Log.d(LOG_TAG, "Argument(s) for hexStringToByteArray(String s)" + "was not a hex string");
    }
    return data;
}