ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


functions:parseint

Table of Contents

ParseInt

int, int ParseInt(str s, int base)

This function returns multiple values

Description

Parses s as an integer in the specified base. If base is 0, base 10 is used by default and Number prefixes are enabled. Supports signs.

Returns status and parsed number. Status is one of the following:

  • PARSENUMBER_SUCCESS – number successfully parsed. r2 contains the result.
  • PARSENUMBER_OVERFLOW – the number is too big to fit into a variable. r2 contains INT_MAX with the sign of the number.
  • PARSENUMBER_BADFORMAT – the number failed to parse. r2 is unchanged.
For simple parsing use atoi.

Examples

ParseInt("123", 0);
r1 -> PARSENUMBER_SUCCESS
r2 -> 123

ParseInt("0", 0);
r1 = PARSENUMBER_SUCCESS
r2 -> 0

ParseInt("999999999999", 0)
r1 -> PARSENUMBER_OVERFLOW
r2 -> 2147483647

ParseInt("-99999999999", 0)
r1 -> PARSENUMBER_OVERFLOW
r2 -> -2147483647

ParseInt("not a number", 0)
r1 -> PARSENUMBER_BADFORMAT

ParseInt("1.3", 0) -> 0
r1 -> PARSENUMBER_BADFORMAT

ParseInt("0xff", 0) // Hexadecimal prefix
r1 -> PARSENUMBER_SUCCESS
r2 -> 255

ParseInt("ff", 16) // Parse in hexadecimal
r1 -> PARSENUMBER_SUCCESS
r2 -> 255

ParseInt("0xff", 16) // 0x causes syntax error
r1 -> PARSENUMBER_BADFORMAT

ParseInt
functions/parseint.txt · Last modified: 2017/04/17 15:38 by korshun