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

public static byte[] decodeQ(byte[] bytes) throws IOException {
    int len = bytes.length;
    int length = 0;
    for (int i = 0; i < len; i++) {
        byte b = bytes[i];
        if (b == '=') {
            i++;//from   w ww  .  j a va  2 s  . c  o  m
            if (i == len)
                break;
            b = bytes[i];
            if (b == '\r' || b == '\n') {
                b = bytes[++i];
                if (b != '\n') {
                    i--;
                }
                continue;
            }
            int result = -Character.digit(b, 16);
            result *= 16;
            result -= Character.digit(bytes[++i], 16);
            bytes[length++] = (byte) -result;
        } else {
            bytes[length++] = b;
        }
    }
    byte[] result = new byte[length];
    System.arraycopy(bytes, 0, result, 0, length);
    return result;
}

From source file:Main.java

protected static int toDigit(char ch, int index) {
    int digit = Character.digit(ch, 16);
    if (digit == -1) {
        return 0;
    } else {/* ww w . jav a 2s .  c om*/
        return digit;
    }
}

From source file:Main.java

public static byte[] decodeQuotedPrintable(final byte[] bytes) {
    if (bytes == null) {
        return null;
    }/*from w  w w . j  av a  2  s . c om*/
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        final int b = bytes[i];
        if (b == '=') {
            try {
                final int u = Character.digit((char) bytes[++i], 16);
                final int l = Character.digit((char) bytes[++i], 16);
                buffer.write((char) ((u << 4) + l));
            } catch (Exception e) {
                //                    FileLog.e("tmessages", e);
                return null;
            }
        } else {
            buffer.write(b);
        }
    }
    byte[] array = buffer.toByteArray();
    try {
        buffer.close();
    } catch (Exception e) {
        //            FileLog.e("tmessages", e);
    }
    return array;
}

From source file:Main.java

/**
 * decode Unicode string/*w  w  w  .j  a  va  2s . co  m*/
 *
 * @param s s
 * @return string
 */
public static String decodeUnicodeStr(String s) {
    StringBuilder sb = new StringBuilder(s.length());
    char[] chars = s.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        char c = chars[i];
        if (c == '\\' && chars[i + 1] == 'u') {
            char cc = 0;
            for (int j = 0; j < 4; j++) {
                char ch = Character.toLowerCase(chars[i + 2 + j]);
                if ('0' <= ch && ch <= '9' || 'a' <= ch && ch <= 'f') {
                    cc |= (Character.digit(ch, 16) << (3 - j) * 4);
                } else {
                    cc = 0;
                    break;
                }
            }
            if (cc > 0) {
                i += 5;
                sb.append(cc);
                continue;
            }
        }
        sb.append(c);
    }
    return sb.toString();
}

From source file:Main.java

public static byte[] hexStringToByteArray(final String s) {
    final int len = s.length();
    final 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));
    }//from   w w  w.  j  a va2s.  c  o  m
    return data;
}

From source file:Main.java

/**
 * Parse an integer located between 2 given offsets in a string
 * //  w w w . ja  v  a 2 s.c om
 * @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
 * @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.substring(beginIndex, endIndex));
        }
        result = -digit;
    }
    while (i < endIndex) {
        digit = Character.digit(value.charAt(i++), 10);
        if (digit < 0) {
            throw new NumberFormatException("Invalid number: " + value.substring(beginIndex, endIndex));
        }
        result *= 10;
        result -= digit;
    }
    return -result;
}

From source file:Main.java

/**
 * Return true if the string represent a number
 * in the specified radix.//from   w  ww. j  a  v a 2  s.  c o  m
 * <br><br>
 **/
public static boolean isNumeric(String s, int radix) {
    int i = 0, len = s.length();
    while (i < len && Character.digit(s.charAt(i), radix) > -1) {
        i++;
    }
    return (i >= len && len > 0);
}

From source file:Main.java

public static byte[] decodeHex(String str) {
    str = str.toLowerCase();/*w  ww. jav  a 2s. c  o m*/
    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));
    }
    return data;
}

From source file:Main.java

/**
 * Performs the Luhn check on the given card number.
 *
 * @param cardNumber a String consisting of numeric digits (only).
 * @return {@code true} if the sequence passes the checksum
 * @throws IllegalArgumentException if {@code cardNumber} contained a non-digit (where {@link
 * Character#isDefined(char)} is {@code false}).
 * @see <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Luhn Algorithm (Wikipedia)</a>
 */// ww w .  j ava2s  . c  o  m
public static boolean isLuhnValid(String cardNumber) {
    final String reversed = new StringBuffer(cardNumber).reverse().toString();
    final int len = reversed.length();
    int oddSum = 0;
    int evenSum = 0;
    for (int i = 0; i < len; i++) {
        final char c = reversed.charAt(i);
        if (!Character.isDigit(c)) {
            throw new IllegalArgumentException(String.format("Not a digit: '%s'", c));
        }
        final int digit = Character.digit(c, 10);
        if (i % 2 == 0) {
            oddSum += digit;
        } else {
            evenSum += digit / 5 + (2 * digit) % 10;
        }
    }
    return (oddSum + evenSum) % 10 == 0;
}

From source file:Main.java

public static byte[] decodeHex(String str) {
    str = str.toLowerCase(Locale.US);
    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));
    }//  www . j a v a2 s.  c  o m
    return data;
}