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.nfcreaderorigin.MainActivity.java

/**
 * Converts the hex./*from w  w  w  .  j ava  2 s. c o m*/
 * 
 * @param buffer
 *            the buffer.
 * @return the HEX string.
 */
public String hexToString(String hex) {

    StringBuilder sb = new StringBuilder();
    String[] splitSpace = hex.split("\\s+");

    hex = "";

    for (int i = 0; i < splitSpace.length; i++) {

        hex = hex + splitSpace[i];

    }

    char[] hexData = hex.toCharArray();

    for (int count = 0; count < hexData.length - 1; count += 2) {

        int firstDigit = Character.digit(hexData[count], 16);

        int lastDigit = Character.digit(hexData[count + 1], 16);

        int decimal = firstDigit * 16 + lastDigit;

        sb.append((char) decimal);
    }
    return sb.toString();
}

From source file:org.apache.wookie.beans.jcr.JCRPersistenceManager.java

/**
 * Unescape invalid JCR characters in name.
 * //w  ww  .j a va 2  s. c o  m
 * @param escapedName escaped name
 * @return original name
 */
public static String unescapeJCRName(String escapedName) {
    StringBuilder unescapedName = null;
    if (escapedName != null) {
        for (int i = 0, limit = escapedName.length(); (i < limit); i++) {
            char escapedNameChar = escapedName.charAt(i);
            if (escapedNameChar == '%') {
                if (unescapedName == null) {
                    unescapedName = new StringBuilder(escapedName.substring(0, i));
                }
                int high = Character.digit(escapedName.charAt(++i), 16);
                int low = Character.digit(escapedName.charAt(++i), 16);
                unescapedName.append((char) (high * 16 + low));
            } else if (unescapedName != null) {
                unescapedName.append(escapedNameChar);
            }
        }
    }
    return ((unescapedName != null) ? unescapedName.toString() : escapedName);
}

From source file:net.ontopia.persistence.query.sql.GenericSQLGenerator.java

protected void referenceSQLFunction(SQLFunction func, StringBuilder sql, BuildInfo info) {
    SQLValueIF[] args = func.getArguments();
    String fname = func.getName();

    if (isPatternFunction(func)) {
        int pix = 0;
        while (true) {
            int ix = fname.indexOf('$', pix);
            if (ix == -1 || ix >= fname.length() - 1) {
                sql.append(fname.substring(pix));
                break;
            } else {
                // FIXME: only 10 arguments supported
                char c = fname.charAt(ix + 1);
                if (Character.isDigit(c)) {
                    int cix = Character.digit(c, 10) - 1;
                    sql.append(fname.substring(pix, ix));
                    atomicSQLValueIF(args[cix], sql, info);
                    ix = ix + 2;//from   w  ww .jav a 2s .  com
                } else {
                    sql.append(fname.substring(pix, ix));
                    ix = ix + 1;
                }
                pix = ix;
            }
        }
    } else {
        if (">".equals(fname) || ">=".equals(fname) || "<=".equals(fname) || "<".equals(fname)) {
            referenceSQLValueIFOpBinary(args[0], fname, args[1], sql, info);
        } else {
            sql.append(func.getName()).append('(');
            for (int i = 0; i < args.length; i++) {
                if (i > 0)
                    sql.append(", ");
                atomicSQLValueIF(args[i], sql, info);
            }
            sql.append(')');
        }
    }
}

From source file:org.eclipse.che.plugin.docker.client.DockerConnector.java

private String getBuildImageId(ProgressStatus progressStatus) {
    final String stream = progressStatus.getStream();
    if (stream != null && stream.startsWith("Successfully built ")) {
        int endSize = 19;
        while (endSize < stream.length() && Character.digit(stream.charAt(endSize), 16) != -1) {
            endSize++;/*w ww.  j  a  v  a  2  s  . c  o m*/
        }
        return stream.substring(19, endSize);
    }
    return null;
}

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

public static char calcChecksum(String value, int length) {
    if (value != null && value.length() == length + 1) {
        value = value.substring(0, length);
    }/*from   w ww .  j  a  v  a2 s  .  c  om*/
    if (value == null || value.length() != length) {
        throw new IllegalArgumentException(
                "Illegal size of value; must be either" + length + " or " + (length + 1) + " characters");
    }
    int oddsum = 0;
    int evensum = 0;
    for (int i = value.length() - 1; i >= 0; i--) {
        if ((value.length() - i) % 2 == 0) {
            evensum += Character.digit(value.charAt(i), 10);
        } else {
            oddsum += Character.digit(value.charAt(i), 10);
        }
    }
    int check = 10 - ((evensum + 3 * oddsum) % 10);
    if (check >= 10)
        check = 0;
    return Character.forDigit(check, 10);
}

From source file:org.fuin.utils4j.Utils4J.java

/**
 * Converts a hexadecimal character to an integer.
 * //  www .  j a  v  a2  s . c  o  m
 * @param ch
 *            A character to convert to an integer digit
 * @param index
 *            The index of the character in the source
 * @return An integer
 * 
 * @author Apache Software Foundation
 * @see org.apache.commons.codec.binary.Hex
 */
private static int toDigit(final char ch, final int index) {
    final int digit = Character.digit(ch, 16);
    if (digit == -1) {
        throw new RuntimeException("Illegal hexadecimal charcter " + ch + " at index " + index);
    }
    return digit;
}

From source file:kr.wdream.storyshop.AndroidUtilities.java

public static byte[] decodeQuotedPrintable(final byte[] bytes) {
    if (bytes == null) {
        return null;
    }/*from   ww w. ja  v  a  2  s  .com*/
    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:com.ferdi2005.secondgram.AndroidUtilities.java

public static byte[] decodeQuotedPrintable(final byte[] bytes) {
    if (bytes == null) {
        return null;
    }/*from  w w w . j  av a  2  s. c o m*/
    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(e);
                return null;
            }
        } else {
            buffer.write(b);
        }
    }
    byte[] array = buffer.toByteArray();
    try {
        buffer.close();
    } catch (Exception e) {
        FileLog.e(e);
    }
    return array;
}

From source file:com.mirth.connect.model.util.ImportConverter3_0_0.java

private 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 .ja v a2 s.co  m*/
    }

    return bytes.toByteArray();
}

From source file:de.innovationgate.utils.WGUtils.java

/**
 * Decodes an encoded URI sequence according to the given charset
 * This is able to decode URL-encoded characters but also leaves existing Non-ASCII characters intact (unlike Common HttpClients URIUtil)
 * @param uriCharSequence The URI sequence
 * @param charset The charset used to decode
 * @return The decoded string/*from  w  ww  .  ja  v a  2  s  . co  m*/
 * @throws UnsupportedEncodingException
 * @throws MalformedURLException
 */
public static String decodeURI(CharSequence uriCharSequence, String charset)
        throws UnsupportedEncodingException, MalformedURLException {

    if (uriCharSequence == null) {
        return null;
    }
    String uri = uriCharSequence.toString();

    int oi = 0; // output index
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    for (int i = 0; i < uri.length(); i++) {
        char c = uri.charAt(i);
        if (c == '%' && i + 2 <= uri.length()) {
            byte high = (byte) Character.digit((char) uri.charAt(++i), 16);
            byte low = (byte) Character.digit((char) uri.charAt(++i), 16);
            if (high == -1 || low == -1) {
                throw new MalformedURLException("Invalid escape pattern");

            }
            byte aByte = (byte) ((high << 4) + low);
            out.write(aByte);
        } else if (c == '+') {
            out.write(' ');
        } else {
            byte[] bytes = String.valueOf(c).getBytes(charset);
            out.write(bytes, 0, bytes.length);
        }
    }

    return out.toString(charset);
}