/** * <p> * Converts a hexadecimal digit into an int using the default (Lsb0) bit ordering. * </p> * <p> * '1' is converted to 1 * </p> * * @param hexDigit the hexadecimal digit to convert * @return an int equals to {@code hexDigit} * @throws IllegalArgumentException if {@code hexDigit} is not a hexadecimal digit */ public static int hexDigitToInt(final char hexDigit) { final int digit = Character.digit(hexDigit, 16); if (digit < 0) { throw new IllegalArgumentException("Cannot interpret '" + hexDigit + "' as a hexadecimal digit"); } return digit; }
private static String convertToAsciiNumber(String convId) { StringBuilder buf = new StringBuilder(convId); for (int i = 0; i < buf.length(); i++) { char ch = buf.charAt(i); int digit = Character.digit(ch, 10); if (digit >= 0) { buf.setCharAt(i, (char) ('0' + digit)); } } return buf.toString(); }
/** * 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. */ 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; }
private static String convertToAsciiNumber(String convId) { StringBuilder buf = new StringBuilder(convId); for (int i = 0; i < buf.length(); i++) { char ch = buf.charAt(i); int digit = Character.digit(ch, 10); if (digit >= 0) { buf.setCharAt(i, (char) ('0' + digit)); } } return buf.toString(); }
public static long parseUnsignedLong(String s, int radix) { int len = s.length(); long first = Long.parseLong(s.substring(0, len - 1), radix); int second = Character.digit(s.charAt(len - 1), radix); return first * radix + second; }
public static long parseUnsignedLong(String s, int radix) { int len = s.length(); long first = Long.parseLong(s.substring(0, len - 1), radix); int second = Character.digit(s.charAt(len - 1), radix); return first * radix + second; }
private static String convertToAsciiNumber(String convId) { StringBuilder buf = new StringBuilder(convId); for (int i = 0; i < buf.length(); i++) { char ch = buf.charAt(i); int digit = Character.digit(ch, 10); if (digit >= 0) { buf.setCharAt(i, (char) ('0' + digit)); } } return buf.toString(); }
public static boolean isInteger(String s) { return isInteger(s,10); } public static 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 continue; } if(Character.digit(s.charAt(i),radix) < 0) return false; } return true; }
private static void verifyUriComponent(@Nullable String source, Type type) { if (source == null) { return; } int length = source.length(); for (int i = 0; i < length; i++) { char ch = source.charAt(i); if (ch == '%') { if ((i + 2) < length) { char hex1 = source.charAt(i + 1); char hex2 = source.charAt(i + 2); int u = Character.digit(hex1, 16); int l = Character.digit(hex2, 16); if (u == -1 || l == -1) { throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\""); } i += 2; } else { throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\""); } } else if (!type.isAllowed(ch)) { throw new IllegalArgumentException("Invalid character '" + ch + "' for " + type.name() + " in \"" + source + "\""); } } }
long value = 0; for (int pos = 0; pos < string.length(); pos++) { int digit = Character.digit(string.charAt(pos), radix); if (digit == -1) { throw new NumberFormatException(string);
public static DecimalBigInt valueOf(String text, int radix) { DecimalBigInt bigRadix = new DecimalBigInt(radix); DecimalBigInt value = new DecimalBigInt(); // 0 for(char digit : text.toCharArray()) { DecimalBigInt bigDigit = new DecimalBigInt(Character.digit(digit, radix)); value = value.times(bigRadix).plus(bigDigit); } return value; }
@VisibleForTesting static int parseRgb(Slice color) { if (color.length() != 4 || color.getByte(0) != '#') { return -1; } int red = Character.digit((char) color.getByte(1), 16); int green = Character.digit((char) color.getByte(2), 16); int blue = Character.digit((char) color.getByte(3), 16); if (red == -1 || green == -1 || blue == -1) { return -1; } // replicate the nibbles to turn a color of the form #rgb => #rrggbb (css semantics) red = (red << 4) | red; green = (green << 4) | green; blue = (blue << 4) | blue; return (int) rgb(red, green, blue); }
} else if (Character.digit(c, 16) == -1) { return null; // Everything else must be a decimal or hex digit.
private int parseInt(int start, int end, int radix, boolean negative) { int max = Integer.MIN_VALUE / radix; int result = 0; int currOffset = start; while (currOffset < end) { int digit = Character.digit((char) (value[currOffset++ + offset] & 0xFF), radix); if (digit == -1) { throw new NumberFormatException(subSequence(start, end, false).toString()); } if (max > result) { throw new NumberFormatException(subSequence(start, end, false).toString()); } int next = result * radix - digit; if (next > result) { throw new NumberFormatException(subSequence(start, end, false).toString()); } result = next; } if (!negative) { result = -result; if (result < 0) { throw new NumberFormatException(subSequence(start, end, false).toString()); } } return result; }
private long parseLong(int start, int end, int radix, boolean negative) { long max = Long.MIN_VALUE / radix; long result = 0; int currOffset = start; while (currOffset < end) { int digit = Character.digit((char) (value[currOffset++ + offset] & 0xFF), radix); if (digit == -1) { throw new NumberFormatException(subSequence(start, end, false).toString()); } if (max > result) { throw new NumberFormatException(subSequence(start, end, false).toString()); } long next = result * radix - digit; if (next > result) { throw new NumberFormatException(subSequence(start, end, false).toString()); } result = next; } if (!negative) { result = -result; if (result < 0) { throw new NumberFormatException(subSequence(start, end, false).toString()); } } return result; }