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.jmangos.auth.wow.controller.AccountController.java

/**
 * Convert to session key./*from   ww w. ja v  a 2  s.  c o  m*/
 * 
 * @param hexkey
 * 
 * @return the byte[]
 */
private byte[] convertSessionKey(final String hexkey) {

    final int len = hexkey.length();
    final byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[((len - i) / 2) - 1] = (byte) ((Character.digit(hexkey.charAt(i), 16) << 4)
                + Character.digit(hexkey.charAt(i + 1), 16));
    }
    return data;
}

From source file:QPEncoderStream.java

/**
 * Convert the bytes within the specified range of the given byte 
 * array into a signed integer in the given radix . The range extends 
 * from <code>start</code> till, but not including <code>end</code>. <p>
 *
 * Based on java.lang.Integer.parseInt()
 *//*ww w  .  j  a  va  2  s . c o  m*/
public static int parseInt(byte[] b, int start, int end, int radix) throws NumberFormatException {
    if (b == null)
        throw new NumberFormatException("null");

    int result = 0;
    boolean negative = false;
    int i = start;
    int limit;
    int multmin;
    int digit;

    if (end > start) {
        if (b[i] == '-') {
            negative = true;
            limit = Integer.MIN_VALUE;
            i++;
        } else {
            limit = -Integer.MAX_VALUE;
        }
        multmin = limit / radix;
        if (i < end) {
            digit = Character.digit((char) b[i++], radix);
            if (digit < 0) {
                throw new NumberFormatException("illegal number: " + toString(b, start, end));
            } else {
                result = -digit;
            }
        }
        while (i < end) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit((char) b[i++], radix);
            if (digit < 0) {
                throw new NumberFormatException("illegal number");
            }
            if (result < multmin) {
                throw new NumberFormatException("illegal number");
            }
            result *= radix;
            if (result < limit + digit) {
                throw new NumberFormatException("illegal number");
            }
            result -= digit;
        }
    } else {
        throw new NumberFormatException("illegal number");
    }
    if (negative) {
        if (i > start + 1) {
            return result;
        } else { /* Only got "-" */
            throw new NumberFormatException("illegal number");
        }
    } else {
        return -result;
    }
}

From source file:eu.stratosphere.nephele.net.NetUtils.java

/**
 * Given a string representation of a host, return its ip address
 * in textual presentation.// w ww . java 2  s  .c  om
 * 
 * @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), 16) != -1) { // it is an IP
        return name;
    } else {
        try {
            InetAddress ipAddress = InetAddress.getByName(name);
            return ipAddress.getHostAddress();
        } catch (UnknownHostException e) {
            return name;
        }
    }
}

From source file:com.mozilla.simplepush.simplepushdemoapp.MainActivity.java

private byte[] hexToBytes(String hex) throws IOException {
    int len = hex.length();
    if (len % 2 == 1) {
        hex = "0" + hex;
        len += 1;/*from  w  w w  . j a v  a2  s  .co m*/
    }
    try {
        byte[] buf = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            buf[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return buf;
    } catch (StringIndexOutOfBoundsException x) {
        throw new IOException("Invalid hex string" + hex);
    }
}

From source file:org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer.java

public static String charSetString(String charSetName, String charSetString) throws SemanticException {
    try {/* w  ww  .  j a  va 2 s .  c om*/
        // The character set name starts with a _, so strip that
        charSetName = charSetName.substring(1);
        if (charSetString.charAt(0) == '\'') {
            return new String(unescapeSQLString(charSetString).getBytes(), charSetName);
        } else // hex input is also supported
        {
            assert charSetString.charAt(0) == '0';
            assert charSetString.charAt(1) == 'x';
            charSetString = charSetString.substring(2);

            byte[] bArray = new byte[charSetString.length() / 2];
            int j = 0;
            for (int i = 0; i < charSetString.length(); i += 2) {
                int val = Character.digit(charSetString.charAt(i), 16) * 16
                        + Character.digit(charSetString.charAt(i + 1), 16);
                if (val > 127) {
                    val = val - 256;
                }
                bArray[j++] = (byte) val;
            }

            String res = new String(bArray, charSetName);
            return res;
        }
    } catch (UnsupportedEncodingException e) {
        throw new SemanticException(e);
    }
}

From source file:main.java.vasolsim.common.GenericUtils.java

/**
 * Converts a hex string to a byte array
 *
 * @param hex the hex string/*from   w ww  .  j  av  a2 s  .co m*/
 *
 * @return byte array
 */
public static byte[] convertHexStringToBytes(String hex) {
    int length = hex.length();
    byte[] data = new byte[length / 2];
    for (int i = 0; i < length; i += 2)
        data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                + Character.digit(hex.charAt(i + 1), 16));

    return data;
}

From source file:com.git.original.common.utils.IPUtils.java

/**
 * Convert IPv6 presentation level address to network order binary form.
 * <p>/*from   ww  w.  j av a 2 s  .c o  m*/
 * credit:
 * <p>
 * Converted from C code from Solaris 8 (inet_pton) Any component of the
 * string following a per-cent % is ignored.
 * <p>
 * : openjdk-7
 * 
 * @param src
 *            a String representing an IPv6 address in textual format
 * @return a byte array representing the IPv6 numeric address
 */
public static byte[] textToNumericFormatV6(String src) {
    // Shortest valid string is "::", hence at least 2 chars
    if (src.length() < 2) {
        return null;
    }
    int colonp;
    char ch;
    boolean sawXDigit;
    int val;
    char[] srcb = src.toCharArray();
    byte[] dst = new byte[INADDR16SZ];
    int srcbLength = srcb.length;
    int pc = src.indexOf("%");
    if (pc == srcbLength - 1) {
        return null;
    }
    if (pc != -1) {
        srcbLength = pc;
    }
    colonp = -1;
    int i = 0, j = 0;
    /* Leading :: requires some special handling. */
    if (srcb[i] == ':') {
        if (srcb[++i] != ':') {
            return null;
        }
    }
    int curtok = i;
    sawXDigit = false;
    val = 0;
    while (i < srcbLength) {
        ch = srcb[i++];
        int chval = Character.digit(ch, 16);
        if (chval != -1) {
            val <<= 4;
            val |= chval;
            if (val > 0xffff) {
                return null;
            }
            sawXDigit = true;
            continue;
        }
        if (ch == ':') {
            curtok = i;
            if (!sawXDigit) {
                if (colonp != -1) {
                    return null;
                }
                colonp = j;
                continue;
            } else if (i == srcbLength) {
                return null;
            }
            if (j + INT16SZ > INADDR16SZ) {
                return null;
            }
            dst[j++] = (byte) ((val >> 8) & 0xff);
            dst[j++] = (byte) (val & 0xff);
            sawXDigit = false;
            val = 0;
            continue;
        }
        if (ch == '.' && ((j + INADDR4SZ) <= INADDR16SZ)) {
            String ia4 = src.substring(curtok, srcbLength);
            /* check this IPv4 address has 3 dots, ie. A.B.C.D */
            int dotCount = 0, index = 0;
            while ((index = ia4.indexOf('.', index)) != -1) {
                dotCount++;
                index++;
            }
            if (dotCount != 3) {
                return null;
            }
            byte[] v4addr = textToNumericFormatV4(ia4);
            if (v4addr == null) {
                return null;
            }
            for (int k = 0; k < INADDR4SZ; k++) {
                dst[j++] = v4addr[k];
            }
            sawXDigit = false;
            break; /* '\0' was seen by inet_pton4(). */
        }
        return null;
    }
    if (sawXDigit) {
        if (j + INT16SZ > INADDR16SZ) {
            return null;
        }
        dst[j++] = (byte) ((val >> 8) & 0xff);
        dst[j++] = (byte) (val & 0xff);
    }
    if (colonp != -1) {
        int n = j - colonp;
        if (j == INADDR16SZ) {
            return null;
        }
        for (i = 1; i <= n; i++) {
            dst[INADDR16SZ - i] = dst[colonp + n - i];
            dst[colonp + n - i] = 0;
        }
        j = INADDR16SZ;
    }
    if (j != INADDR16SZ) {
        return null;
    }
    byte[] newdst = convertFromIPv4MappedAddress(dst);
    if (newdst != null) {
        return newdst;
    } else {
        return dst;
    }
}

From source file:eap.util.EDcodeUtil.java

public static byte[] hexDecode(CharSequence s) {
    int nChars = s.length();

    if (nChars % 2 != 0) {
        throw new IllegalArgumentException("Hex-encoded string must have an even number of characters");
    }/*w  w  w  .  ja  v  a2  s. com*/

    byte[] result = new byte[nChars / 2];

    for (int i = 0; i < nChars; i += 2) {
        int msb = Character.digit(s.charAt(i), 16);
        int lsb = Character.digit(s.charAt(i + 1), 16);

        if (msb < 0 || lsb < 0) {
            throw new IllegalArgumentException("Non-hex character in input: " + s);
        }
        result[i / 2] = (byte) ((msb << 4) | lsb);
    }
    return result;
}

From source file:itdelatrisu.opsu.Utils.java

/**
 * Returns the git hash of the remote-tracking branch 'origin/master' from the
 * most recent update to the working directory (e.g. fetch or successful push).
 * @return the 40-character SHA-1 hash, or null if it could not be determined
 */// w  ww  .j av  a 2s  .  com
@Nullable
public static String getGitHash() {
    if (env.isJarRunning)
        return null;
    File f = new File(".git/refs/remotes/origin/master");
    if (!f.isFile())
        f = new File("../.git/refs/remotes/origin/master");
    if (!f.isFile())
        return null;
    try (BufferedReader in = new BufferedReader(new FileReader(f))) {
        char[] sha = new char[40];
        if (in.read(sha, 0, sha.length) < sha.length)
            return null;
        for (int i = 0; i < sha.length; i++) {
            if (Character.digit(sha[i], 16) == -1)
                return null;
        }
        return String.valueOf(sha);
    } catch (IOException e) {
        return null;
    }
}

From source file:CodePointInputMethod.java

private int getCodePoint(StringBuffer sb, int from, int to) {
    int value = 0;
    for (int i = from; i <= to; i++) {
        value = (value << 4) + Character.digit(sb.charAt(i), 16);
    }/*from   www.  j  a v a2  s  . com*/
    return value;
}