Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Text /
Chapter 5 - Text Utilities / Text Utilities Reference
Routines / Converting Between Integers and Strings


StringToNum

The StringToNum procedure 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
StringToNum converts the base-10 numeric string in the theString parameter to the corresponding long integer value and returns the result in the parameter theNum. 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. StringToNum performs 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 by StringToNum:
Value of theString
Value returned
in theNum
"-23"-23
"-0"0
"055"55
"2147483648" (magnitude is 2^31)-2147483648
"-2147483648"-2147483648
"4294967295" (magnitude is 2^32-1)-1
"-4294967295"1

StringToNum does 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, StringToNum converts 2: to the number 30 since the character code for the colon (:) is $3A. Because StringToNum operates 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
theString
Value returned
in theNum
'CAT'314
'+CAT'314
'-CAT'-314

Note
One consequence of this conversion method is that StringToNum does 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 in theString are valid digits before you call StringToNum.
SPECIAL CONSIDERATIONS
StringToNum may move memory; your application should not call this procedure at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION
The trap macro and routine selector for the StringToNum procedure are
Trap macroRoutine selector
_Pack7$0001

The registers on entry and exit for this routine are
Registers on entry
A0pointer to the length byte that precedes theString
Registers on exit
D0the long word value


Previous Book Contents Book Index Next

© Apple Computer, Inc.
6 JUL 1996