parseInteger(X, type, [radix=10])

Alias: parseInt


X is a STRING scalar, vector, pair or matrix.

type specifies the data type of the integer, which can be CHAR, SHORT, INT or LONG.

radix (optional) is an integer in [2, 16] that represents the radix (the base in mathematical numeral systems) of the integer. The default value is 10.

  • If radix=2, X allows 0, 1.

  • If radix=11, X allows A/a (case insensitive) and values in [0, 9].

  • If radix=16, X allows leading character 0x/0X.


parseInteger parses X into an integer of the specified type in specified radix. If it encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. An exception will be thrown if the first character is invalid.

Return value: A CHAR/SHORT/INT/LONG scalar/vector/pair/matrix of the same shape as X.

  • Leading whitespaces or zeros are ignored during parsing.

  • Decimal values are not supported, i.e., the point . is considered illegal.

  • An empty string is parsed as NULL.

  • The sign character +/- indicates positive or negative only if it appears after a leading whitespace, otherwise it is considered illegal.


parseInteger([" ", "000", "012", " 12","12a", "1a2","+12a", "-12"], INT)
//output: [ , 0, 12, 12, 12, 1, 12, -12]

parseInteger("a12", INT)
//Error: 'Invalid string to parse.'

parseInteger(["012", " 12","12a", "1a2","1A2", "-1A2"], INT, 11)
//output: [13, 13, 153, 233, 233, -233]

parseInteger(["0x16", "0X16"], INT)
//output:[0, 0]

parseInteger(["0x16", "0X16"], INT, 16)
//output:[22, 22]

parseInteger( "9" , INT, 8)
//Error: 'Invalid string to parse.'