List of usage examples for java.lang Character digit
public static int digit(int codePoint, int radix)
From source file:Main.java
/** * Converts a string consisting of hex-coded signs into an byte-array. * /* ww w .jav a 2s. c o m*/ * @param hexString the input string with values in hex representation. * @return a byte array holding the hex-values in the expected format. */ public 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:Main.java
private static int parse(char[] chars, int offset, int len, int radix, boolean negative) throws NumberFormatException { int max = Integer.MIN_VALUE / radix; int result = 0; for (int i = 0; i < len; i++) { int digit = Character.digit(chars[i + offset], radix); if (digit == -1) { throw new NumberFormatException("Unable to parse"); }/*from w ww . j a va 2s .c o m*/ if (max > result) { throw new NumberFormatException("Unable to parse"); } int next = result * radix - digit; if (next > result) { throw new NumberFormatException("Unable to parse"); } result = next; } /*while (offset < len) { }*/ if (!negative) { result = -result; if (result < 0) { throw new NumberFormatException("Unable to parse"); } } return result; }
From source file:Main.java
/** * Returns a byte array containing values parsed from the hex string provided. * * @param hexString The hex string to convert to bytes. * @return A byte array containing values parsed from the hex string provided. *///from w ww.j a v a 2s . c om public static byte[] getBytesFromHexString(String hexString) { byte[] data = new byte[hexString.length() / 2]; for (int i = 0; i < data.length; i++) { int stringOffset = i * 2; data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4) + Character.digit(hexString.charAt(stringOffset + 1), 16)); } return data; }
From source file:Main.java
private static int parse(char[] string, int start, int length, int offset, int radix, boolean negative) throws NumberFormatException { int max = Integer.MIN_VALUE / radix; int result = 0; while (offset < length) { int digit = Character.digit(string[start + (offset++)], radix); if (digit == -1) { throw new NumberFormatException(new String(string, start, length)); }//from w w w . jav a 2 s.com if (max > result) { throw new NumberFormatException(new String(string, start, length)); } int next = result * radix - digit; if (next > result) { throw new NumberFormatException(new String(string, start, length)); } result = next; } if (!negative) { result = -result; if (result < 0) { throw new NumberFormatException(new String(string, start, length)); } } return result; }
From source file:Main.java
/** * @param b source byte array/*from w w w . j a v a2s. c o m*/ * @param offset starting offset * @param len number of bytes in destination (processes len*2) * @return byte[len] */ public static byte[] hex2byte(byte[] b, int offset, int len) { byte[] d = new byte[len]; for (int i = 0; i < len * 2; i++) { int shift = i % 2 == 1 ? 0 : 4; d[i >> 1] |= Character.digit((char) b[offset + i], 16) << shift; } return d; }
From source file:Main.java
/** * Takes a HEX stream and returns the corresponding byte array. * //w w w . jav a 2 s . c o m * @param hexStream * the HEX stream. * @return the byte array. */ public static byte[] hexStreamToByteArray(String hexStream) { int length = hexStream.length(); byte[] data = new byte[length / 2]; for (int i = 0; i < length; i += 2) { data[i / 2] = (byte) ((Character.digit(hexStream.charAt(i), 16) << 4) + Character.digit(hexStream.charAt(i + 1), 16)); } return data; }
From source file:Main.java
/** * Converts an ASCII representation of a Bitmap field * into a Java BitSet/* w w w.j av a 2 s .co m*/ * @param b - hex representation * @param offset - starting offset * @param bitZeroMeansExtended - true for ISO-8583 * @return java BitSet object */ public static BitSet hex2BitSet(byte[] b, int offset, boolean bitZeroMeansExtended) { int len = bitZeroMeansExtended ? ((Character.digit((char) b[offset], 16) & 0x08) == 8 ? 128 : 64) : 64; BitSet bmap = new BitSet(len); for (int i = 0; i < len; i++) { int digit = Character.digit((char) b[offset + (i >> 2)], 16); if ((digit & (0x08 >> (i % 4))) > 0) bmap.set(i + 1); } return bmap; }
From source file:org.openhab.binding.miio.internal.Utils.java
/** * Convert a string representation of hexadecimal to a byte array. * * For example: String s = "00010203" returned byte array is {0x00, 0x01, 0x03} * * @param hex hex input string// ww w.ja v a 2 s .c o m * @return byte array equivalent to hex string **/ public static byte[] hexStringToByteArray(String hex) { String s = hex.replace(" ", ""); 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:Main.java
/** * Normalize a phone number by removing the characters other than digits. If * the given number has keypad letters, the letters will be converted to * digits first./*from ww w. j a va2s.c o m*/ * * @param phoneNumber The number to be normalized. * @return The normalized number. * * TODO: Remove if PhoneNumberUtils.normalizeNumber(String phoneNumber) is made public. */ public static String normalizeNumber(String phoneNumber) { StringBuilder sb = new StringBuilder(); int len = phoneNumber.length(); for (int i = 0; i < len; i++) { char c = phoneNumber.charAt(i); // Character.digit() supports ASCII and Unicode digits (fullwidth, Arabic-Indic, etc.) int digit = Character.digit(c, 10); if (digit != -1) { sb.append(digit); } else if (i == 0 && c == '+') { sb.append(c); } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { return normalizeNumber(PhoneNumberUtils.convertKeypadLettersToDigits(phoneNumber)); } } return sb.toString(); }
From source file:Main.java
/** * Converts a String to an integer of base radix. * <br><br>//ww w . j av a 2 s. c om * String constraints are: * <li>Number must be less than 10 digits</li> * <li>Number must be positive</li> * @param s String representation of number * @param radix Number base to use * @return integer value of number * @throws NumberFormatException */ public static int parseInt(String s, int radix) throws NumberFormatException { int length = s.length(); if (length > 9) throw new NumberFormatException("Number can have maximum 9 digits"); int result = 0; int index = 0; int digit = Character.digit(s.charAt(index++), radix); if (digit == -1) throw new NumberFormatException("String contains non-digit"); result = digit; while (index < length) { result *= radix; digit = Character.digit(s.charAt(index++), radix); if (digit == -1) throw new NumberFormatException("String contains non-digit"); result += digit; } return result; }