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:org.apache.spark.sql.parser.SemanticAnalyzer.java

@SuppressWarnings("nls")
public static String unescapeSQLString(String b) {
    Character enclosure = null;/*from w  w w.  j  a  va  2 s .  c  o  m*/

    // Some of the strings can be passed in as unicode. For example, the
    // delimiter can be passed in as \002 - So, we first check if the
    // string is a unicode number, else go back to the old behavior
    StringBuilder sb = new StringBuilder(b.length());
    for (int i = 0; i < b.length(); i++) {

        char currentChar = b.charAt(i);
        if (enclosure == null) {
            if (currentChar == '\'' || b.charAt(i) == '\"') {
                enclosure = currentChar;
            }
            // ignore all other chars outside the enclosure
            continue;
        }

        if (enclosure.equals(currentChar)) {
            enclosure = null;
            continue;
        }

        if (currentChar == '\\' && (i + 6 < b.length()) && b.charAt(i + 1) == 'u') {
            int code = 0;
            int base = i + 2;
            for (int j = 0; j < 4; j++) {
                int digit = Character.digit(b.charAt(j + base), 16);
                code += digit * multiplier[j];
            }
            sb.append((char) code);
            i += 5;
            continue;
        }

        if (currentChar == '\\' && (i + 4 < b.length())) {
            char i1 = b.charAt(i + 1);
            char i2 = b.charAt(i + 2);
            char i3 = b.charAt(i + 3);
            if ((i1 >= '0' && i1 <= '1') && (i2 >= '0' && i2 <= '7') && (i3 >= '0' && i3 <= '7')) {
                byte bVal = (byte) ((i3 - '0') + ((i2 - '0') * 8) + ((i1 - '0') * 8 * 8));
                byte[] bValArr = new byte[1];
                bValArr[0] = bVal;
                String tmp = new String(bValArr);
                sb.append(tmp);
                i += 3;
                continue;
            }
        }

        if (currentChar == '\\' && (i + 2 < b.length())) {
            char n = b.charAt(i + 1);
            switch (n) {
            case '0':
                sb.append("\0");
                break;
            case '\'':
                sb.append("'");
                break;
            case '"':
                sb.append("\"");
                break;
            case 'b':
                sb.append("\b");
                break;
            case 'n':
                sb.append("\n");
                break;
            case 'r':
                sb.append("\r");
                break;
            case 't':
                sb.append("\t");
                break;
            case 'Z':
                sb.append("\u001A");
                break;
            case '\\':
                sb.append("\\");
                break;
            // The following 2 lines are exactly what MySQL does TODO: why do we do this?
            case '%':
                sb.append("\\%");
                break;
            case '_':
                sb.append("\\_");
                break;
            default:
                sb.append(n);
            }
            i++;
        } else {
            sb.append(currentChar);
        }
    }
    return sb.toString();
}

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

private int calculateCheckDigit(char[] chars) {
    int oddSum = 0;
    for (int i = 1; i < chars.length - 1; i += 2) {
        oddSum += Character.digit(chars[i], DIGIT);
    }//w w  w.j  a va  2s . co  m
    int evenSum = 0;
    for (int i = 0; i < chars.length - 1; i += 2) {
        evenSum += Character.digit(chars[i], DIGIT);
    }
    int totalSum = oddSum + evenSum;
    return (DIGIT - (totalSum % DIGIT)) % DIGIT;
}

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

public static byte[] decodeNextHex(String data) {
    byte[] out = new byte[1];

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

    return out;
}

From source file:Main.java

/**
 * Substitute index identifiers with the replacement value from the
 * given array for the corresponding index.
 *
 * @param buff       The string buffer used for the substitution
 *                   (buffer is not reset).
 * @param string     String substitution format.
 * @param replace    Array of strings whose values will be used as 
 *                   replacements in the given string when a token with
 *                   their index is found.
 * @param token      The character token to specify the start of an index
 *                   reference.//w  ww.ja  v  a 2s  . co  m
 * @return           Substituted string.
 */
public static String subst(final StringBuffer buff, final String string, final String replace[],
        final char token) {
    int i = string.length();
    for (int j = 0; j >= 0 && j < i; j++) {
        char c = string.charAt(j);

        // if the char is the token, then get the index
        if (c == token) {

            // if we aren't at the end of the string, get the index
            if (j != i) {
                int k = Character.digit(string.charAt(j + 1), 10);

                if (k == -1) {
                    buff.append(string.charAt(j + 1));
                } else if (k < replace.length) {
                    buff.append(replace[k]);
                }

                j++;
            }
        } else {
            buff.append(c);
        }
    }

    return buff.toString();
}

From source file:com.xunlei.util.codec.Hex.java

/**
 * Converts a hexadecimal character to an integer.
 * //  w w w.  jav a  2  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
 * @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 charcter " + ch + " at index " + index);
    }
    return digit;
}

From source file:hu.webhejj.commons.text.StringUtils.java

protected static int toHexDigit(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);
    }// w  w w .  j  av a  2  s  .  co  m
    return digit;
}

From source file:com.ligadata.EncryptUtils.EncryptionUtil.java

/**
 * Convert given hex string to byte array
 * //w  ww  .j  av a 2s .  c  om
 * @param s
 *          : a string containing hex characters
 * @return byte array
 * @throws java.lang.Exception an exception is thrown
 */

public static byte[] hexStringToByteArray(String s) throws Exception {
    try {
        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;
    } catch (Exception e) {
        logger.error("Failed to convert hexString to byte array", e);
        throw e;
    }
}

From source file:wptools.lib.Misc.java

private static byte[] parseFing(String s) {
    String s2 = s.replaceAll(":", "");
    int len = s2.length();
    if (len != MD5_LEN * 2 && len != SHA1_LEN * 2 && len != SHA256_LEN * 2)
        Misc.die("bad fingerprint: " + s);
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        int v0 = Character.digit(s2.charAt(i), 16);
        int v1 = Character.digit(s2.charAt(i + 1), 16);
        if (v0 < 0 || v1 < 0)
            Misc.die("bad fingerprint: " + s);
        data[i / 2] = (byte) ((v0 << 4) | v1);
    }/*  w w  w.  j  a v a2 s. c  o m*/
    return data;
}

From source file:org.activiti.designer.eclipse.navigator.cloudrepo.ActivitiCloudEditorUtil.java

/**
 * Converts hex values from strings to byte arra
 *
 * @param hexString string of hex-encoded values
 * @return decoded byte array/*from   ww  w  .ja v a 2s  . com*/
 */
protected static byte[] hexStringToByteArray(String hexString) {
    int len = hexString.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                + Character.digit(hexString.charAt(i + 1), 16));
    }
    return data;
}

From source file:gool.generator.common.CommonCodeGenerator.java

/**
 * <pre>/*from  w  w  w  . j  ava2s.  c  om*/
 * Produce indented code in a manner similar to printf but with custom conversions.
 * %%  a single "%"
 * %s  Print an argument as a string, without indentation or newlines added.
 *     Similar to the corresponding flag of <i>String.format</i>.
 * %<i>n</i>  (where <i>n</i> is a digit)
 *     Print an argument as a bloc indented <i>n</i> times from the current indentation level.
 *     Newlines are inserted before and after the bloc.
 * %-<i>n</i> (where <i>n</i> is a digit)
 *     <i>n</i> times the indentation string, does not consumes a argument.
 *     %-0 becomes a empty string (it does nothing but is still parsed)
 * 
 * @param format
 *            the format string
 * @param arguments
 *            the objects to format, each one corresponding to a % code
 * @return the formated string
 */
protected String formatIndented(String format, Object... arguments) {
    StringBuilder sb = new StringBuilder(format);
    int pos = sb.indexOf("%");
    int arg = 0;
    while (pos != -1) {
        if (sb.charAt(pos + 1) == '%') {
            sb = sb.replace(pos, pos + 2, "%");
        } else if (sb.charAt(pos + 1) == 's') {
            sb = sb.replace(pos, pos + 2, arguments[arg].toString());
            pos += arguments[arg].toString().length() - 1;
            arg++;
        } else if (Character.isDigit(sb.charAt(pos + 1))) {
            String replacement = ("\n" + arguments[arg].toString().replaceFirst("\\s*\\z", "")).replace("\n",
                    "\n" + StringUtils.repeat(indentation, Character.digit(sb.charAt(pos + 1), 10))) + "\n";
            sb = sb.replace(pos, pos + 2, replacement);
            pos += replacement.length() - 1;
            arg++;
        } else if (sb.charAt(pos + 1) == '-' && Character.isDigit(sb.charAt(pos + 2))) {
            String replacement = StringUtils.repeat(indentation, Character.digit(sb.charAt(pos + 2), 10));
            sb = sb.replace(pos, pos + 3, replacement);
            pos += replacement.length();
        }
        pos = sb.indexOf("%", pos);
    }
    return sb.toString();
}