/** * @param c An ASCII encoded character 0-9 a-f A-F * @return The byte value of the character 0-16. */ public static int convertHexDigit(char c) { int d = ((c & 0x1f) + ((c >> 6) * 0x19) - 0x10); if (d < 0 || d > 15) { throw new NumberFormatException("!hex " + c); } return d; }
/** * @param c An ASCII encoded character 0-9 a-f A-F * @return The byte value of the character 0-16. */ public static int convertHexDigit(int c) { int d = ((c & 0x1f) + ((c >> 6) * 0x19) - 0x10); if (d < 0 || d > 15) { throw new NumberFormatException("!hex " + c); } return d; }
private static short parseHextet(String ipPart) { // Note: we already verified that this string contains only hex digits. int hextet = Integer.parseInt(ipPart, 16); if (hextet > 0xffff) { throw new NumberFormatException(); } return (short) hextet; }
private static NumberFormatException _badBD(String s) { return new NumberFormatException("Value \""+s+"\" can not be represented as BigDecimal"); } }
private static NumberFormatException _badBD(String s) { return new NumberFormatException("Value \""+s+"\" can not be represented as BigDecimal"); } }
private static int digit( char ch ) { int digit = ch - '0'; if ( (digit < 0) || (digit > 9) ) { throw new NumberFormatException(); } return digit; }
private static byte parseOctet(String ipPart) { // Note: we already verified that this string contains only hex digits. int octet = Integer.parseInt(ipPart); // Disallow leading zeroes, because no clear standard exists on // whether these should be interpreted as decimal or octal. if (octet > 255 || (ipPart.startsWith("0") && ipPart.length() > 1)) { throw new NumberFormatException(); } return (byte) octet; }
private static short parseHextet(String ipPart) { // Note: we already verified that this string contains only hex digits. int hextet = Integer.parseInt(ipPart, 16); if (hextet > 0xffff) { throw new NumberFormatException(); } return (short) hextet; }
private static byte parseOctet(String ipPart) { // Note: we already verified that this string contains only hex digits. int octet = Integer.parseInt(ipPart); // Disallow leading zeroes, because no clear standard exists on // whether these should be interpreted as decimal or octal. if (octet > 255 || (ipPart.startsWith("0") && ipPart.length() > 1)) { throw new NumberFormatException(); } return (byte) octet; }
/** * Returns the unsigned {@code int} value represented by a string with the given radix. * * <p><b>Java 8 users:</b> use {@link Integer#parseUnsignedInt(String, int)} instead. * * @param string the string containing the unsigned integer representation to be parsed. * @param radix the radix to use while parsing {@code s}; must be between {@link * Character#MIN_RADIX} and {@link Character#MAX_RADIX}. * @throws NumberFormatException if the string does not contain a valid unsigned {@code int}, or * if supplied radix is invalid. * @throws NullPointerException if {@code s} is null (in contrast to {@link * Integer#parseInt(String)}) */ @CanIgnoreReturnValue public static int parseUnsignedInt(String string, int radix) { checkNotNull(string); long result = Long.parseLong(string, radix); if ((result & INT_MASK) != result) { throw new NumberFormatException( "Input " + string + " in base " + radix + " is not in the range of an unsigned integer"); } return (int) result; }
/** * Returns the unsigned {@code byte} value represented by a string with the given radix. * * @param string the string containing the unsigned {@code byte} representation to be parsed. * @param radix the radix to use while parsing {@code string} * @throws NumberFormatException if the string does not contain a valid unsigned {@code byte} with * the given radix, or if {@code radix} is not between {@link Character#MIN_RADIX} and {@link * Character#MAX_RADIX}. * @throws NullPointerException if {@code string} is null (in contrast to {@link * Byte#parseByte(String)}) * @since 13.0 */ @Beta @CanIgnoreReturnValue public static byte parseUnsignedByte(String string, int radix) { int parse = Integer.parseInt(checkNotNull(string), radix); // We need to throw a NumberFormatException, so we have to duplicate checkedCast. =( if (parse >> Byte.SIZE == 0) { return (byte) parse; } else { throw new NumberFormatException("out of range: " + parse); } }
static ParseRequest fromString(String stringValue) { if (stringValue.length() == 0) { throw new NumberFormatException("empty string"); } // Handle radix specifier if present String rawValue; int radix; char firstChar = stringValue.charAt(0); if (stringValue.startsWith("0x") || stringValue.startsWith("0X")) { rawValue = stringValue.substring(2); radix = 16; } else if (firstChar == '#') { rawValue = stringValue.substring(1); radix = 16; } else if (firstChar == '0' && stringValue.length() > 1) { rawValue = stringValue.substring(1); radix = 8; } else { rawValue = stringValue; radix = 10; } return new ParseRequest(rawValue, radix); } }
public int parseInt(int start, int end, int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException(); } if (start == end) { throw new NumberFormatException(); } int i = start; boolean negative = byteAt(i) == '-'; if (negative && ++i == end) { throw new NumberFormatException(subSequence(start, end, false).toString()); } return parseInt(i, end, radix, negative); }
public long parseLong(int start, int end, int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException(); } if (start == end) { throw new NumberFormatException(); } int i = start; boolean negative = byteAt(i) == '-'; if (negative && ++i == end) { throw new NumberFormatException(subSequence(start, end, false).toString()); } return parseLong(i, end, radix, negative); }
@Override public int getIntHeader(String name) { HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); Object value = (header != null ? header.getValue() : null); if (value instanceof Number) { return ((Number) value).intValue(); } else if (value instanceof String) { return Integer.parseInt((String) value); } else if (value != null) { throw new NumberFormatException("Value for header '" + name + "' is not a Number: " + value); } else { return -1; } }
public void setTouchy(String touchy) throws Exception { if (touchy.indexOf('.') != -1) { throw new Exception("Can't contain a ."); } if (touchy.indexOf(',') != -1) { throw new NumberFormatException("Number format exception: contains a ,"); } this.touchy = touchy; }
public short parseShort(int start, int end, int radix) { int intValue = parseInt(start, end, radix); short result = (short) intValue; if (result != intValue) { throw new NumberFormatException(subSequence(start, end, false).toString()); } return result; }
public short parseShort(int start, int end, int radix) { int intValue = parseInt(start, end, radix); short result = (short) intValue; if (result != intValue) { throw new NumberFormatException(subSequence(start, end, false).toString()); } return result; }
@Override public int getIntHeader(String name) { HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); Object value = (header != null ? header.getValue() : null); if (value instanceof Number) { return ((Number) value).intValue(); } else if (value instanceof String) { return Integer.parseInt((String) value); } else if (value != null) { throw new NumberFormatException("Value for header '" + name + "' is not a Number: " + value); } else { return -1; } }
/** * Returns the unsigned {@code int} value represented by the given string. * * <p>Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix: * * <ul> * <li>{@code 0x}<i>HexDigits</i> * <li>{@code 0X}<i>HexDigits</i> * <li>{@code #}<i>HexDigits</i> * <li>{@code 0}<i>OctalDigits</i> * </ul> * * @throws NumberFormatException if the string does not contain a valid unsigned {@code int} value * @since 13.0 */ @CanIgnoreReturnValue public static int decode(String stringValue) { ParseRequest request = ParseRequest.fromString(stringValue); try { return parseUnsignedInt(request.rawValue, request.radix); } catch (NumberFormatException e) { NumberFormatException decodeException = new NumberFormatException("Error parsing value: " + stringValue); decodeException.initCause(e); throw decodeException; } }