Instant methods: parse_uint32()

The instant method parse_uint32() converts the unsigned integral number stored in the string operand in the corresponding uint32 value.

Declaration

Form 1:

uint32 string.parse_uint32( uint32 aDefault )

Form 2:

uint32 string.parse_uint32( uint32 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_uint32() method parses the string operand interpreting its content as an integral number. 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 uint32 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 uint32 r1 = s1.parse_uint32( 0 ); // r1 = 1369 // Skip over leading whitespace signs and ignore the signs // following the number 1251 var uint32 r2 = s2.parse_uint32( 0 ); // r2 = 1251 // The number doesn't start with an expected sign. Thereupon // return the default value. var uint32 r3 = s3.parse_uint32( 0 ); // r3 = 0

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 uint32 r1 = s1.parse_uint32( 0, 2 ); // r1 = 112 // Parse number in octal notation var uint32 r2 = s2.parse_uint32( 0, 8 ); // r2 = 32648 // Parse number in hexadecimal notation var uint32 r3 = s3.parse_uint32( 0, 16 ); // r3 = 42996