Instant methods: parse_int64()
The instant method parse_int64() converts the signed integral number stored in the string operand in the corresponding 64-bit signed int64 value.
Declaration
Form 1:
int64 string.parse_int64( int64 aDefault )
Form 2:
int64 string.parse_int64( int64 aDefault, int32 aRadix )
Parameters
aDefault
Value to return if the string doesn't contain any valid number.
aRadix
Determines the format of the number stored in the string. This parameter can assume values 2, 8, 10 or 16. Accordingly the string content is considered as being a number with binary, octal, decimal or hexadecimal notation. When the passed value is other than 2, 8 or 16, the value 10 is assumed implicitly.
Discussion
The parse_int64() method parses the string operand interpreting its content as an integral number. The number can be prefixed by an optional + (plus) or - (minus) sign. Eventual whitespace signs ' ' (space), '\t' (tabulator), '\n' (newline), '\v' (vertical tab), '\f' (form-feed) and '\r' (carriage return) lying at the begin of the string are ignored and skipped over.
Generally the method tries to parse as many signs as possible. If the end of the string or an unexpected sign is found, the method stops and returns the already read number as signed int64 value. If the string content is invalid (the string doesn't contain any of the expected digit or letter signs), the value passed in the parameter aDefault is returned instead. For example:
var string s1 = "+1369"; var string s2 = " -1251-other content"; var string s3 = "-X251"; var int64 r1 = s1.parse_int64( -1 ); // r1 = 1369 // Skip over leading whitespace signs and ignore the signs // following the number 1251 var int64 r2 = s2.parse_int64( -1 ); // r2 = -1251 // The number doesn't start with an expected sign. Thereupon // return the default value. var int64 r3 = s3.parse_int64( -1 ); // r3 = -1
In its first form, the method interprets the string content as a number stored in decimal notation. With the second form, you can specify in the additional parameter aRadix the expected notation of the number. The parameter aRadix can assume values 2, 8, 10 or 16 identifying that the string contains a number in binary, octal, decimal or hexadecimal notation. For example:
var string s1 = "+01110000"; var string s2 = "-77610"; var string s3 = "-A7f4"; // Parse number in binary notation var int64 r1 = s1.parse_int64( -1, 2 ); // r1 = 112 // Parse number in octal notation var int64 r2 = s2.parse_int64( -1, 8 ); // r2 = -32648 // Parse number in hexadecimal notation var int64 r3 = s3.parse_int64( -1, 16 ); // r3 = -42996