/** * Returns the unsigned {@code long} value represented by the given decimal string. * * <p><b>Java 8 users:</b> use {@link Long#parseUnsignedLong(String)} instead. * * @throws NumberFormatException if the string does not contain a valid unsigned {@code long} * value * @throws NullPointerException if {@code string} is null (in contrast to {@link * Long#parseLong(String)}) */ @CanIgnoreReturnValue public static long parseUnsignedLong(String string) { return parseUnsignedLong(string, 10); }
/** * Returns an {@code UnsignedLong} holding the value of the specified {@code String}, parsed as an * unsigned {@code long} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} * value, or {@code radix} is not between {@link Character#MIN_RADIX} and {@link * Character#MAX_RADIX} */ @CanIgnoreReturnValue public static UnsignedLong valueOf(String string, int radix) { return fromLongBits(UnsignedLongs.parseUnsignedLong(string, radix)); }
/** * Returns the unsigned {@code long} value represented by the given decimal string. * * <p><b>Java 8 users:</b> use {@link Long#parseUnsignedLong(String)} instead. * * @throws NumberFormatException if the string does not contain a valid unsigned {@code long} * value * @throws NullPointerException if {@code string} is null (in contrast to {@link * Long#parseLong(String)}) */ @CanIgnoreReturnValue public static long parseUnsignedLong(String string) { return parseUnsignedLong(string, 10); }
/** * Returns the unsigned {@code long} value represented by the given decimal string. * * <p><b>Java 8 users:</b> use {@link Long#parseUnsignedLong(String)} instead. * * @throws NumberFormatException if the string does not contain a valid unsigned {@code long} * value * @throws NullPointerException if {@code string} is null (in contrast to {@link * Long#parseLong(String)}) */ @CanIgnoreReturnValue public static long parseUnsignedLong(String string) { return parseUnsignedLong(string, 10); }
public void testParseLongFails() { try { // One more than maximum value UnsignedLongs.parseUnsignedLong("18446744073709551616"); fail(); } catch (NumberFormatException expected) { } }
public void testParseLong() { assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("18446744073709551615")); assertEquals(0x7fffffffffffffffL, UnsignedLongs.parseUnsignedLong("9223372036854775807")); assertEquals(0xff1a618b7f65ea12L, UnsignedLongs.parseUnsignedLong("18382112080831834642")); assertEquals(0x5a4316b8c153ac4dL, UnsignedLongs.parseUnsignedLong("6504067269626408013")); assertEquals(0x6cf78a4b139a4e2aL, UnsignedLongs.parseUnsignedLong("7851896530399809066")); }
public void testParseLongEmptyString() { try { UnsignedLongs.parseUnsignedLong(""); fail("NumberFormatException should have been raised."); } catch (NumberFormatException expected) { } }
public void testParseLongWithRadix() { assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("ffffffffffffffff", 16)); assertEquals(0x1234567890abcdefL, UnsignedLongs.parseUnsignedLong("1234567890abcdef", 16)); }
/** * Returns an {@code UnsignedLong} holding the value of the specified {@code String}, parsed as an * unsigned {@code long} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} * value, or {@code radix} is not between {@link Character#MIN_RADIX} and {@link * Character#MAX_RADIX} */ @CanIgnoreReturnValue public static UnsignedLong valueOf(String string, int radix) { return fromLongBits(UnsignedLongs.parseUnsignedLong(string, radix)); }
/** * Returns the unsigned {@code long} 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 long} * value * @since 13.0 */ @CanIgnoreReturnValue public static long decode(String stringValue) { ParseRequest request = ParseRequest.fromString(stringValue); try { return parseUnsignedLong(request.rawValue, request.radix); } catch (NumberFormatException e) { NumberFormatException decodeException = new NumberFormatException("Error parsing value: " + stringValue); decodeException.initCause(e); throw decodeException; } }
/** * Returns an {@code UnsignedLong} holding the value of the specified {@code String}, parsed as an * unsigned {@code long} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} * value, or {@code radix} is not between {@link Character#MIN_RADIX} and {@link * Character#MAX_RADIX} */ @CanIgnoreReturnValue public static UnsignedLong valueOf(String string, int radix) { return fromLongBits(UnsignedLongs.parseUnsignedLong(string, radix)); }
public void testParseLongThrowsExceptionForInvalidRadix() { // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX, inclusive. try { UnsignedLongs.parseUnsignedLong("0", Character.MIN_RADIX - 1); fail(); } catch (NumberFormatException expected) { } try { UnsignedLongs.parseUnsignedLong("0", Character.MAX_RADIX + 1); fail(); } catch (NumberFormatException expected) { } // The radix is used as an array index, so try a negative value. try { UnsignedLongs.parseUnsignedLong("0", -1); fail(); } catch (NumberFormatException expected) { } }
public void testParseLongWithRadixLimits() { BigInteger max = BigInteger.ZERO.setBit(64).subtract(ONE); // loops through all legal radix values. for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { // tests can successfully parse a number string with this radix. String maxAsString = max.toString(radix); assertEquals(max.longValue(), UnsignedLongs.parseUnsignedLong(maxAsString, radix)); try { // tests that we get exception whre an overflow would occur. BigInteger overflow = max.add(ONE); String overflowAsString = overflow.toString(radix); UnsignedLongs.parseUnsignedLong(overflowAsString, radix); fail(); } catch (NumberFormatException expected) { } } try { UnsignedLongs.parseUnsignedLong("1234567890abcdef1", 16); fail(); } catch (NumberFormatException expected) { } }
/** * Returns the unsigned {@code long} 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 long} * value * @since 13.0 */ @CanIgnoreReturnValue public static long decode(String stringValue) { ParseRequest request = ParseRequest.fromString(stringValue); try { return parseUnsignedLong(request.rawValue, request.radix); } catch (NumberFormatException e) { NumberFormatException decodeException = new NumberFormatException("Error parsing value: " + stringValue); decodeException.initCause(e); throw decodeException; } }
/** * Returns the unsigned {@code long} 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 long} * value * @since 13.0 */ @CanIgnoreReturnValue public static long decode(String stringValue) { ParseRequest request = ParseRequest.fromString(stringValue); try { return parseUnsignedLong(request.rawValue, request.radix); } catch (NumberFormatException e) { NumberFormatException decodeException = new NumberFormatException("Error parsing value: " + stringValue); decodeException.initCause(e); throw decodeException; } }
/** * Converts a String to a UUID * * @param uuid The string to be converted * @return The result */ public static UUID getUUID(String uuid) { return new UUID( UnsignedLongs.parseUnsignedLong( uuid.substring( 0, 16 ), 16 ), UnsignedLongs.parseUnsignedLong( uuid.substring( 16 ), 16 ) ); } }
/** * Converts a string from hex to long. * * @param string hex number in string form; sans 0x * @return long value */ public static long fromHex(String string) { return UnsignedLongs.parseUnsignedLong(string, 16); }
@Override Object protoFromSoy(SoyValue field) { return UnsignedLongs.parseUnsignedLong(field.stringValue()); } };
/** * Returns an {@code UnsignedLong} holding the value of the specified {@code String}, parsed as * an unsigned {@code long} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} * value, or {@code radix} is not between {@link Character#MIN_RADIX} and * {@link Character#MAX_RADIX} */ public static UnsignedLong valueOf(String string, int radix) { return fromLongBits(UnsignedLongs.parseUnsignedLong(string, radix)); }
public void testParseLong() { assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("18446744073709551615")); assertEquals(0x7fffffffffffffffL, UnsignedLongs.parseUnsignedLong("9223372036854775807")); assertEquals(0xff1a618b7f65ea12L, UnsignedLongs.parseUnsignedLong("18382112080831834642")); assertEquals(0x5a4316b8c153ac4dL, UnsignedLongs.parseUnsignedLong("6504067269626408013")); assertEquals(0x6cf78a4b139a4e2aL, UnsignedLongs.parseUnsignedLong("7851896530399809066")); }