Function in conv coil-stdlib

parse_int_radix

fn parse_int_radix(string s, int radix) -> Result<int, string>
Import
use conv::{parse_int_radix};
Source: src/conv.hy

Parses an integer written in base radix, which must be 10 or 16.

Surrounding ASCII whitespace and one leading + or - are accepted. Hex digits may be either case; there is no 0x prefix.

Parameters#

NameType
sstring
radixint

Returns#

Result<int, string>

Errors#

Err("invalid integer") for empty or non-digit text and Err("invalid radix") for any other radix. A value that does not fit in an int currently panics instead of returning an error (coil-stdlib#53).

Examples#

use conv::{parse_int_radix};
use tests::assert;
assert!((parse_int_radix("ff", 16) ?? 0) == 255);
assert!((parse_int_radix("-10", 10) ?? 0) == -10);
assert!(parse_int_radix("12", 8).is_err());

See also#

  • int
  • int_to_dec: Formats n in decimal; the same as format("%i", n).
  • int_to_hex: Formats n in lowercase hexadecimal without a prefix; a negative n gets a leading -.
  • int_to_bin: Formats n in binary without a prefix; a negative n gets a leading -.
  • parse_int: Parses a decimal integer, such as "42", " -7 " or "+3".
  • parse_float: Parses a decimal float such as "3.5", "-0.25", "1e3" or "42".
↑ ↓ navigate ↵ open