Type Conversion Functions

The macro preprocessor provides automatic type conversion between lists, strings, and integers. To summarize, numeric strings are converted to integers using a standard (character) decimal-to-binary conversion. Non-numeric strings are converted to zeros. Integers are converted to strings by using a standard binary-to-decimal conversion that produces no leading zeros. Conversions between lists and the other two types are detailed in Section 12.

Automatic type conversions are supplemented by several functions that do explicit type conversions. Integers can be converted to fixed-length strings using the 'itos function. The 'itos function requires two arguments, the first of which is the integer to be converted, and the second of which is the width of the number to be produced. If the converted string is shorter than the specified width, it is padded on the left with zeros. If it is longer than the specified width, characters on the left are truncated.

In addition to the decimal conversions, the functions 'xtoi, 'otoi, 'btoi, 'hex, 'octal, and 'binary can be used to do conversions using the bases 16, 8, and 2. The function 'xtoi converts a string containing digits and the letters a-f (or A-F) into an integer. The characters are assumed to represent base-16 digits. Similarly the functions 'otoi and 'btoi can be used to convert strings containing the characters 0-7 (for 'otoi) or 0-1 (for 'btoi) into integers. The characters of the string are assumed to be octal (for 'otoi) or binary (for 'btoi) digits. The functions 'hex, 'octal, and 'binary perform integer to string conversions. The function 'hex produces a base-16 number using the additional digits a-f, while 'octal and 'binary produce base-8 and base-2 numbers respectively.

Integer constants may be specified in decimal, octal, or hexadecimal. Decimal numbers other than zero begin with a non-zero digit and contain only the digits 0-9. Octal numbers begin with a zero and contain only the digits 0-7. Hexadecimal numbers begin with the two characters "0x" or "0X" and thereafter contain only the characters 0-9, a-f, and A-F. Note that the operand of 'xtoi must not contain the leading "0x" or "0X" characters. Furthermore, the operand of 'otoi need not start with a zero. Furthermore, the function 'hex will not prepend "0x" or "0X" to its output, nor will the output of 'octal necessarily start with a zero.

If the functions 'hex, 'octal, and 'binary are supplied with a single operand, the result string will begin with a non-zero digit, and will be just long enough to contain all significant digits of the converted value. If a second operand is supplied, it must be an integer specifying the number of digits to be produced. The result string will be padded with zeros or truncated on the left to produce a string of the specified length.

The function 'ctoi can be used to convert a character to an integer whose value is the binary value of the character in the underlying character representation. This is ascii for most systems, so 'ctoi(" ") usually returns the number 32. The operand of 'ctoi is a string. All characters but the first are ignored.