gix_utils::btoi

Function to_signed_with_radix

source
pub fn to_signed_with_radix<I: MinNumTraits>(
    bytes: &[u8],
    radix: u32,
) -> Result<I, ParseIntegerError>
Expand description

Converts a byte slice in a given base to an integer.

Like to_unsigned_with_radix, but numbers may optionally start with a sign (- or +).

§Errors

Returns ParseIntegerError for any of the following conditions:

  • bytes has no digits
  • not all characters of bytes are 0-9, a-z, A-Z, excluding an optional leading sign
  • not all characters refer to digits in the given radix, excluding an optional leading sign
  • the number overflows or underflows I

§Panics

Panics if radix is not in the range 2..=36 (or in the pathological case that there is no representation of radix in I).

§Examples

assert_eq!(Ok(10), to_signed_with_radix(b"a", 16));
assert_eq!(Ok(10), to_signed_with_radix(b"+a", 16));
assert_eq!(Ok(-42), to_signed_with_radix(b"-101010", 2));