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.hadoop.net.NetUtils.java

/** 
 * Given a string representation of a host, return its ip address
 * in textual presentation.//from  w w w .ja  v  a 2s .  c o  m
 * 
 * @param name a string representation of a host:
 *             either a textual representation its IP address or its host name
 * @return its IP address in the string format
 */
public static String normalizeHostName(String name) {
    if (Character.digit(name.charAt(0), 10) != -1) { //FIXME 
        return name;
    } else {
        try {
            InetAddress ipAddress = InetAddress.getByName(name);
            return ipAddress.getHostAddress();
        } catch (UnknownHostException e) {
            return name;
        }
    }
}

From source file:org.openhab.binding.sonance.internal.SonanceBinding.java

/**
 * Function to convert strings to hexadecimal bytes.
 *
 * @param s/*from ww w.j av a  2 s  .com*/
 *            the string to convert to a hexadecimal byte array
 * @return hexadecimal byte array
 */
private 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));
    }
    return data;
}

From source file:com.aerospike.load.AsWriterTask.java

public byte[] toByteArray(String s) {

    if ((s.length() % 2) != 0) {
        log.error("blob exception: " + s);
        throw new IllegalArgumentException(
                "Input hex formated string must contain an even number of characters");
    }//from w w w . j  a v a2s  . c  om

    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.error("blob exception:" + e);
    }
    return data;
}

From source file:com.cloudbees.hudson.plugins.folder.ChildNameGeneratorRecTest.java

/**
 * Inverse function of {@link Util#rawEncode(String)}
 *
 * @param s the encoded string./*ww  w  . j  a  v a  2s.  c o  m*/
 * @return the decoded string.
 */
@NonNull
public static String rawDecode(@NonNull String s) {
    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:org.apache.pdfbox.preflight.parser.PreflightParser.java

/**
 * Check that the hexa string contains only an even number of
 * Hexadecimal characters. Once it is done, reset the offset at the beginning of the string and
 * call {@link PDFParser#parseCOSString()}
 *
 * @return The parsed PDF string./*from   w w w.  j a v a  2s.c  o m*/
 *
 * @throws IOException If there is an error reading from the stream.
 */
@Override
protected COSString parseCOSString() throws IOException {
    // offset reminder
    long offset = source.getPosition();
    char nextChar = (char) source.read();
    int count = 0;
    if (nextChar == '<') {
        do {
            nextChar = (char) source.read();
            if (nextChar != '>') {
                if (isWhitespace(nextChar)) {
                    // ignore space characters
                    continue;
                }
                if (Character.digit(nextChar, 16) >= 0) {
                    count++;
                } else {
                    addValidationError(new ValidationError(ERROR_SYNTAX_HEXA_STRING_INVALID,
                            "Hexa String must have only Hexadecimal Characters (found '" + nextChar
                                    + "') at offset " + source.getPosition()));
                    break;
                }
            }
        } while (nextChar != '>');
    }

    if (count % 2 != 0) {
        addValidationError(new ValidationError(ERROR_SYNTAX_HEXA_STRING_EVEN_NUMBER,
                "Hexa string shall contain even number of non white space char at offset "
                        + source.getPosition()));
    }

    // reset the offset to parse the COSString
    source.seek(offset);
    COSString result = super.parseCOSString();

    if (result.getString().length() > MAX_STRING_LENGTH) {
        addValidationError(new ValidationError(ERROR_SYNTAX_HEXA_STRING_TOO_LONG,
                "Hexa string is too long at offset " + source.getPosition()));
    }
    return result;
}

From source file:org.kchine.rpf.PoolUtils.java

public static final byte[] hexToBytes(String s) throws NumberFormatException, IndexOutOfBoundsException {
    int slen = s.length();
    if ((slen % 2) != 0) {
        s = '0' + s;
    }/*w  w  w .  j a v a  2  s. c o  m*/

    byte[] out = new byte[slen / 2];

    byte b1, b2;
    for (int i = 0; i < slen; i += 2) {
        b1 = (byte) Character.digit(s.charAt(i), 16);
        b2 = (byte) Character.digit(s.charAt(i + 1), 16);
        if ((b1 < 0) || (b2 < 0)) {
            throw new NumberFormatException();
        }
        out[i / 2] = (byte) (b1 << 4 | b2);
    }
    return out;
}

From source file:org.orekit.propagation.analytical.tle.TLE.java

/** Compute the checksum of the first 68 characters of a line.
 * @param line line to check/*from w  w w . j av  a 2s.  c o  m*/
 * @return checksum
 */
private static int checksum(final CharSequence line) {
    int sum = 0;
    for (int j = 0; j < 68; j++) {
        final char c = line.charAt(j);
        if (Character.isDigit(c)) {
            sum += Character.digit(c, 10);
        } else if (c == '-') {
            ++sum;
        }
    }
    return sum % 10;
}

From source file:org.pentaho.platform.repository.RepositoryFilenameUtils.java

/**
 * Reverts modifications of {@link #escape(String)} such that for all {@code String}s {@code t},
 * {@code t.equals(unescape(escape(t)))}. Assumes only ASCII characters have been escaped.
 * //from w w w  .j a va2s .  c  om
 * @param name
 *          name to unescape
 * @return unescaped name
 */
public static String unescape(final String name) {
    if (name == null) {
        throw new IllegalArgumentException();
    }
    StringBuilder buffer = new StringBuilder(name.length());
    String str = name;
    int i = str.indexOf(escapeChar);
    while (i > -1 && i + 2 < str.length()) {
        buffer.append(str.toCharArray(), 0, i);
        int a = Character.digit(str.charAt(i + 1), 16);
        int b = Character.digit(str.charAt(i + 2), 16);
        if (a > -1 && b > -1) {
            buffer.append((char) (a * 16 + b));
            str = str.substring(i + 3);
        } else {
            buffer.append(escapeChar);
            str = str.substring(i + 1);
        }
        i = str.indexOf(escapeChar);
    }
    buffer.append(str);
    return buffer.toString();
}

From source file:org.lwes.util.NumberCodec.java

/**
 * Turn an unsigned hex string into a long.  This whole thing is here
 * to replace Long.parseLong(str,16) because it fails with large unsigned
 * hex strings (ones that denote negative values).  When they work out
 * the signed hex vs. unsigned hex issue in the Java API, this can be
 * retired./*from  w  w w .  j  a va  2s  .com*/
 *
 * @param str the String to parse
 * @return the long that was written in <code>str</code>
 */
private static long fromHexString(String str, long min) {
    final int hex = 16;
    final char firstChar = str.charAt(0);
    final int digit = Character.digit(firstChar, hex);
    if (digit < hex / 2) {
        return Long.parseLong(str, hex);
    } else {
        /* Subtract <code>hex/2</code> from the first digit and flip the sign. */
        final String posStr = (Character.forDigit(digit - hex / 2, hex) + str.substring(1, str.length()));
        final long offsetLong = Long.parseLong(posStr, hex);
        return offsetLong + min;
    }
}

From source file:com.antsdb.saltedfish.sql.mysql.ExprGenerator.java

private static byte[] mysqlXBinaryToBytes(String text) {
    if (text.length() < 3) {
        throw new IllegalArgumentException("invalid binary format: " + text);
    }// w  w  w  .j a  v a 2s .  co  m
    if (text.length() % 2 != 1) {
        throw new IllegalArgumentException("invalid binary format: " + text);
    }
    byte[] bytes = new byte[(text.length() - 3) / 2];
    for (int i = 2; i < text.length() - 1; i += 2) {
        int ch1 = text.charAt(i);
        int ch2 = text.charAt(i + 1);
        ch1 = Character.digit(ch1, 16);
        ch2 = Character.digit(ch2, 16);
        int n = ch1 << 4 | ch2;
        bytes[i / 2 - 1] = (byte) n;
    }
    return bytes;
}