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

/**
 * Converts a byte array to an integer of base radix.
 * <br><br>// ww w . j ava 2s.com
 * Array constraints are:
 * <li>Number must be less than 10 digits</li>
 * <li>Number must be positive</li>
 * @param bArray Byte Array representation of number
 * @param radix Number base to use
 * @return integer value of number
 * @throws NumberFormatException
 */
public static int parseInt(byte[] bArray, int radix) throws NumberFormatException {
    int length = bArray.length;
    if (length > 9)
        throw new NumberFormatException("Number can have maximum 9 digits");
    int result = 0;
    int index = 0;
    int digit = Character.digit((char) bArray[index++], radix);
    if (digit == -1)
        throw new NumberFormatException("Byte array contains non-digit");
    result = digit;
    while (index < length) {
        result *= radix;
        digit = Character.digit((char) bArray[index++], radix);
        if (digit == -1)
            throw new NumberFormatException("Byte array contains non-digit");
        result += digit;
    }
    return result;
}

From source file:org.tdmx.lib.control.service.UniqueIdServiceImpl.java

@Override
public boolean isValid(String accountId) {
    if (!StringUtils.hasText(accountId)) {
        throw new IllegalArgumentException("missing accountId");
    }/*from   ww  w .  j  a v a2 s  .com*/
    String oid = accountId;
    char[] chars = oid.toCharArray();
    if (chars.length <= 1) {
        return false;
    }
    int digit = calculateCheckDigit(chars);
    int providedDigit = Character.digit(chars[chars.length - 1], DIGIT);
    return providedDigit == digit;
}

From source file:URLCodec.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.//w ww  .  j  a  v  a2 s .c om
 *
 * @param bytes array of URL safe characters
 * @return array of original bytes 
 * @throws DecoderException Thrown if URL decoding is unsuccessful
 */
public static final byte[] decodeUrl(byte[] bytes) throws Exception {
    if (bytes == null) {
        return null;
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        if (b == '+') {
            buffer.write(' ');
        } else if (b == '%') {
            try {
                int u = Character.digit((char) bytes[++i], 16);
                int l = Character.digit((char) bytes[++i], 16);
                if (u == -1 || l == -1) {
                    throw new RuntimeException("Invalid URL encoding");
                }
                buffer.write((char) ((u << 4) + l));
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new RuntimeException("Invalid URL encoding");
            }
        } else {
            buffer.write(b);
        }
    }
    return buffer.toByteArray();
}

From source file:de.jfachwert.pruefung.Mod10Verfahren.java

private int getQuersumme(String wert) {
    char[] digits = wert.toCharArray();
    int sum = 0;/*  ww  w.  j  a  va 2s.c om*/
    int length = digits.length;
    for (int i = 0; i < length; i++) {
        int digit = Character.digit(digits[i], 10);
        if (i % 2 == 0) {
            digit *= this.gewichtungUngerade;
        } else {
            digit *= this.gewichtungGerade;
        }
        sum += digit;
    }
    return sum;
}

From source file:phex.util.URLCodec.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  .  jav a 2 s .  c o  m*/
 *
 * @param bytes array of URL safe characters
 * @return array of original bytes
 * @throws phex.util.DecoderException Thrown if URL decoding is unsuccessful
 */
public static final byte[] decodeUrl(byte[] bytes) throws phex.util.DecoderException {
    if (bytes == null) {
        return null;
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        if (b == '+') {
            buffer.write(' ');
        } else if (b == '%') {
            try {
                int u = Character.digit((char) bytes[++i], 16);
                int l = Character.digit((char) bytes[++i], 16);
                if (u == -1 || l == -1) {
                    throw new phex.util.DecoderException("Invalid URL encoding");
                }
                buffer.write((char) ((u << 4) + l));
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new phex.util.DecoderException("Invalid URL encoding");
            }
        } else {
            buffer.write(b);
        }
    }
    return buffer.toByteArray();
}

From source file:com.example.android.navigationdrawer.QRCode.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));
    }/*  ww  w.j a  va  2  s . com*/

    return data;
}

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

private <S extends Enum<S> & ServiceCodeType> S serviceCode(final int position, final S defaultServiceCode) {
    if (serviceCode.length() > position) {
        final int value = Character.digit(serviceCode.charAt(position), 10);
        final S[] serviceCodes = defaultServiceCode.getDeclaringClass().getEnumConstants();
        for (final S serviceCode : serviceCodes) {
            if (serviceCode.getValue() == value) {
                return serviceCode;
            }//from  w ww . j  a  v a  2s. c o  m
        }
    }
    return defaultServiceCode;
}

From source file:org.eleusoft.uri.apache.URLCodec.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 w ww . j a v a 2 s .co  m
 *
 * @param bytes array of URL safe characters
 * @return array of original bytes
 * @throws CodecException Thrown if URL decoding is unsuccessful
 */
public static final byte[] decodeUrl(byte[] bytes, boolean plusIsSpace) throws CodecException {
    if (bytes == null) {
        return null;
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        if (b == '+' && plusIsSpace) {
            buffer.write(' ');
        } else if (b == '%') {
            try {
                int u = Character.digit((char) bytes[++i], 16);
                int l = Character.digit((char) bytes[++i], 16);
                if (u == -1 || l == -1) {
                    throw new CodecException("Invalid URL encoding");
                }
                buffer.write((char) ((u << 4) + l));
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new CodecException("Invalid URL encoding");
            }
        } else {
            buffer.write(b);
        }
    }
    return buffer.toByteArray();
}

From source file:org.apache.camel.component.syslog.Rfc3164SyslogConverter.java

public static SyslogMessage parseMessage(byte[] bytes) {
    ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
    byteBuffer.put(bytes);//from   ww w  .ja va  2 s  .c  o m
    byteBuffer.rewind();

    SyslogMessage syslogMessage = new SyslogMessage();
    Character charFound = (char) byteBuffer.get();

    while (charFound != '<') {
        //Ignore noise in beginning of message.
        charFound = (char) byteBuffer.get();
    }
    char priChar = 0;
    if (charFound == '<') {
        int facility = 0;

        while (Character.isDigit(priChar = (char) (byteBuffer.get() & 0xff))) {
            facility *= 10;
            facility += Character.digit(priChar, 10);
        }
        syslogMessage.setFacility(SyslogFacility.values()[facility >> 3]);
        syslogMessage.setSeverity(SyslogSeverity.values()[facility & 0x07]);
    }

    if (priChar != '>') {
        //Invalid character - this is not a well defined syslog message.
        LOG.error("Invalid syslog message, missing a > in the Facility/Priority part");
    }

    //Done parsing severity and facility
    //<169>Oct 22 10:52:01 TZ-6 scapegoat.dmz.example.org 10.1.2.3 sched[0]: That's All Folks!
    //Need to parse the date.

    /**
     The TIMESTAMP field is the local time and is in the format of "Mmm dd
     hh:mm:ss" (without the quote marks) where:
            
     Mmm is the English language abbreviation for the month of the
     year with the first character in uppercase and the other two
     characters in lowercase.  The following are the only acceptable
     values:
            
     Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
            
     dd is the day of the month.  If the day of the month is less
     than 10, then it MUST be represented as a space and then the
     number.  For example, the 7th day of August would be
     represented as "Aug  7", with two spaces between the "g" and
     the "7".
            
     hh:mm:ss is the local time.  The hour (hh) is represented in a
     24-hour format.  Valid entries are between 00 and 23,
     inclusive.  The minute (mm) and second (ss) entries are between
     00 and 59 inclusive.
            
            
     */

    char[] month = new char[3];
    for (int i = 0; i < 3; i++) {
        month[i] = (char) (byteBuffer.get() & 0xff);
    }
    charFound = (char) byteBuffer.get();
    if (charFound != ' ') {
        //Invalid Message - missing mandatory space.
        LOG.error("Invalid syslog message, missing a mandatory space after month");
    }
    charFound = (char) (byteBuffer.get() & 0xff);

    int day = 0;
    if (charFound == ' ') {
        //Extra space for the day - this is okay.
        //Just ignored per the spec.
    } else {
        day *= 10;
        day += Character.digit(charFound, 10);
    }

    while (Character.isDigit(charFound = (char) (byteBuffer.get() & 0xff))) {
        day *= 10;
        day += Character.digit(charFound, 10);
    }

    int hour = 0;
    while (Character.isDigit(charFound = (char) (byteBuffer.get() & 0xff))) {
        hour *= 10;
        hour += Character.digit(charFound, 10);
    }

    int minute = 0;
    while (Character.isDigit(charFound = (char) (byteBuffer.get() & 0xff))) {
        minute *= 10;
        minute += Character.digit(charFound, 10);
    }

    int second = 0;
    while (Character.isDigit(charFound = (char) (byteBuffer.get() & 0xff))) {
        second *= 10;
        second += Character.digit(charFound, 10);
    }

    //The host is the char sequence until the next ' '

    StringBuilder host = new StringBuilder();
    while ((charFound = (char) (byteBuffer.get() & 0xff)) != ' ') {
        host.append(charFound);
    }

    syslogMessage.setHostname(host.toString());

    StringBuilder msg = new StringBuilder();
    while (byteBuffer.hasRemaining()) {
        charFound = (char) (byteBuffer.get() & 0xff);
        msg.append(charFound);
    }

    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.MONTH, monthValueMap.get(String.valueOf(month).toLowerCase()).ordinal());
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, second);

    syslogMessage.setTimestamp(calendar.getTime());

    syslogMessage.setLogMessage(msg.toString());
    if (LOG.isTraceEnabled()) {
        LOG.trace("Syslog message : " + syslogMessage.toString());
    }

    return syslogMessage;
}

From source file:org.lilyproject.repository.impl.AbstractSchemaCache.java

/**
 * Decodes a string containing 2 characters representing a hex value.
 * <p/>/*from w ww.j  a  va 2  s . c  o m*/
 * The returned byte[] contains the byte represented by the string and the next byte.
 * <p/>
 * This code is based on {@link Hex#decodeHex(char[])}
 * <p/>
 */
public static byte[] decodeHexAndNextHex(String data) {
    byte[] out = new byte[2];

    // two characters form the hex value.
    int f = Character.digit(data.charAt(0), 16) << 4;
    f = f | Character.digit(data.charAt(1), 16);
    out[0] = (byte) (f & 0xFF);
    out[1] = (byte) ((f + 1) & 0xFF);

    return out;
}