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.jmeter.protocol.tcp.sampler.BinaryTCPClientImpl.java

/**
 * Convert hex string to binary byte array.
 *
 * @param hexEncodedBinary - hex-encoded binary string
 * @return Byte array containing binary representation of input hex-encoded string
 * @throws IllegalArgumentException if string is not an even number of hex digits
 */// w w w .j a  v  a 2s  .c  o m
public static byte[] hexStringToByteArray(String hexEncodedBinary) {
    if (hexEncodedBinary.length() % 2 == 0) {
        char[] sc = hexEncodedBinary.toCharArray();
        byte[] ba = new byte[sc.length / 2];

        for (int i = 0; i < ba.length; i++) {
            int nibble0 = Character.digit(sc[i * 2], 16);
            int nibble1 = Character.digit(sc[i * 2 + 1], 16);
            if (nibble0 == -1 || nibble1 == -1) {
                throw new IllegalArgumentException(
                        "Hex-encoded binary string contains an invalid hex digit in '" + sc[i * 2]
                                + sc[i * 2 + 1] + "'");
            }
            ba[i] = (byte) ((nibble0 << 4) | (nibble1));
        }

        return ba;
    } else {
        throw new IllegalArgumentException("Hex-encoded binary string contains an uneven no. of digits");
    }
}

From source file:Main.java

/**
 * Converts an ASCII representation of a Bitmap field
 * into a Java BitSet/*from  w ww  .j a v a 2 s  .co  m*/
 * @param bmap - BitSet
 * @param b - hex representation
 * @param bitOffset - (i.e. 0 for primary bitmap, 64 for secondary)
 * @return java BitSet object
 */
public static BitSet hex2BitSet(BitSet bmap, byte[] b, int bitOffset) {
    int len = b.length << 2;
    for (int i = 0; i < len; i++) {
        int digit = Character.digit((char) b[i >> 2], 16);
        if ((digit & (0x08 >> (i % 4))) > 0)
            bmap.set(bitOffset + i + 1);
    }
    return bmap;
}

From source file:org.yestech.lib.util.DecoderUtil.java

private static void uriDecode(final StringBuffer buffer, final int offset, final int length) {
    int index = offset;
    int count = length;
    for (; count > 0; count--, index++) {
        final char ch = buffer.charAt(index);
        if (ch != '%') {
            continue;
        }/*from w w w .  j a va  2 s  .  com*/
        if (count < 3) {
            throw new RuntimeException(
                    "invalid-escape-sequence.error: " + buffer.substring(index, index + count));
        }

        // Decode
        int dig1 = Character.digit(buffer.charAt(index + 1), 16);
        int dig2 = Character.digit(buffer.charAt(index + 2), 16);
        if (dig1 == -1 || dig2 == -1) {
            throw new RuntimeException("invalid-escape-sequence.error " + buffer.substring(index, index + 3));
        }
        char value = (char) (dig1 << 4 | dig2);

        // Replace
        buffer.setCharAt(index, value);
        buffer.delete(index + 1, index + 3);
        count -= 2;
    }
}

From source file:io.seldon.vw.VwFeatureHash.java

private boolean isInteger(String s, int radix) {
    if (s.isEmpty())
        return false;
    for (int i = 0; i < s.length(); i++) {
        if (i == 0 && s.charAt(i) == '-') {
            if (s.length() == 1)
                return false;
            else//from w ww.j a  va 2 s . c  om
                continue;
        }
        if (Character.digit(s.charAt(i), radix) < 0)
            return false;
    }
    return true;
}

From source file:HexUtil.java

/**
 * Converts a String of hex characters into an array of bytes.
 * /*ww  w  . j a va 2 s . co  m*/
 * @param s
 *            A string of hex characters (upper case or lower) of even
 *            length.
 * @param out
 *            A byte array of length at least s.length()/2 + off
 * @param off
 *            The first byte to write of the array
 */
public static final void hexToBytes(String s, byte[] out, int off)
        throws NumberFormatException, IndexOutOfBoundsException {

    int slen = s.length();
    if ((slen % 2) != 0) {
        s = '0' + s;
    }

    if (out.length < off + slen / 2) {
        throw new IndexOutOfBoundsException(
                "Output buffer too small for input (" + out.length + '<' + off + slen / 2 + ')');
    }

    // Safe to assume the string is even length
    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[off + i / 2] = (byte) (b1 << 4 | b2);
    }
}

From source file:org.apache.myriad.state.utils.ByteBufferSupportTest.java

private static byte[] getByteArray(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));
    }//  w w w .j av  a  2  s . c om
    return data;
}

From source file:Main.java

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

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

    if (end > start) {
        if (b[i] == '-') {
            negative = true;
            limit = Long.MIN_VALUE;
            i++;
        } else {
            limit = -Long.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:org.openhab.binding.lifx.internal.fields.MACAddress.java

private byte[] parseHexBinary(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));
    }//from   ww w . j  a v  a 2  s .com
    return data;
}

From source file:Main.java

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

From source file:com.distimo.sdk.Utils.java

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));
    }//from  w w w. j  av  a 2  s. co m
    return data;
}