Skip to main content

Mojo function

atol

atol(str: String, base: Int = 10) -> Int

Parses and returns the given string as an integer in the given base.

If base is set to 0, the string is parsed as an Integer literal, with the following considerations:

  • '0b' or '0B' prefix indicates binary (base 2)
  • '0o' or '0O' prefix indicates octal (base 8)
  • '0x' or '0X' prefix indicates hexadecimal (base 16)
  • Without a prefix, it's treated as decimal (base 10)

Examples: >>> atol("32") 32 >>> atol("FF", 16) 255 >>> atol("0xFF", 0) 255 >>> atol("0b1010", 0) 10

Notes: This follows Python's integer literals.

Args:

  • str (String): A string to be parsed as an integer in the given base.
  • base (Int): Base used for conversion, value must be between 2 and 36, or 0.

Returns:

An integer value that represents the string.

Raises:

If the given string cannot be parsed as an integer value or if an incorrect base is provided.