Important: The information in this document is obsolete and should not be used for new development.
StringToNum
TheStringToNumprocedure converts the Pascal string representation of a base-10 number into a long integer value.
PROCEDURE StringToNum (theString: Str255; VAR theNum: LongInt);
theString- A Pascal string representation of a base-10 number.
theNum- On output, contains the numeric value.
DESCRIPTION
StringToNumconverts the base-10 numeric string in thetheStringparameter to the corresponding long integer value and returns the result in the parametertheNum. The numeric string can be padded with leading zeros or with a sign.The 32-bit result is negated if the string begins with a minus sign. Integer overflow occurs if the magnitude is greater than or equal to 2 raised to the 31st power.
StringToNumperforms the negation using the two's complement method: the state of each bit is reversed and then 1 is added to the result. For example, here are possible results produced byStringToNum:
Value of theStringValue returned
intheNum"-23" -23 "-0" 0 "055" 55 "2147483648" (magnitude is 2^31) -2147483648 "-2147483648" -2147483648 "4294967295" (magnitude is 2^32-1) -1 "-4294967295" 1
StringToNumdoes not check whether the characters in the string are between 0 and 9; instead, it takes advantage of the fact that the ASCII values for these characters are $30 through $39, and masks the last four bits for use as a digit. For example,StringToNumconverts 2: to the number 30 since the character code for the colon (:) is $3A. BecauseStringToNumoperates this way, spaces are treated as zeros (the character code for a space is $20), and other characters do get converted into numbers. For example, the character codes for 'C', 'A', and 'T' are $43, $41, and $54 respectively, producing
these results:
Value of theStringValue returned
intheNum'CAT' 314 '+CAT' 314 '-CAT' -314
- Note
- One consequence of this conversion method is that
StringToNumdoes not ignore thousand separators (the "," character in the United States), which can lead to improper conversions. It is a good idea to ensure that all characters intheStringare valid digits before you callStringToNum.![]()
SPECIAL CONSIDERATIONS
StringToNummay move memory; your application should not call this procedure at interrupt time.ASSEMBLY-LANGUAGE INFORMATION
The trap macro and routine selector for theStringToNumprocedure are
Trap macro Routine selector _Pack7 $0001 The registers on entry and exit for this routine are
Registers on entry A0 pointer to the length byte that precedes theString
Registers on exit D0 the long word value