Go to the first, previous, next, last section, table of contents.


Integer/String Conversion

The OOC Library supplies various procedures to convert between string values and numeric representation. These include procedures for conversions of both INTEGER and LONGINT variables to and from string format.

As module IntConv is low-level, the average application programmer will most likely find module IntStr more interesting and useful.

Module ConvTypes

Module ConvTypes declares common types, and appropriate related constants, which are used in the various string conversion modules.

Data type: ConvResults = SHORTINT
Values of type ConvResults are used to express the status of attempts to format a string via the string-to-number conversion procedures. The following constants are defined for its value:

Constant: strAllRight
The string format is correct for the corresponding conversion.

Constant: strOutOfRange
The string is well-formed but the value cannot be represented.

Constant: strWrongFormat
The string is in the wrong format for the conversion.

Constant: strEmpty
The given string is empty.

Data type: ScanClass = SHORTINT
Values of the type ScanClass are used to classify input to finite state scanners. The following constants are defined for its value:

Constant: padding
A leading or padding character at this point in the scan - ignore it.

Constant: valid
A valid character at this point in the scan - accept it.

Constant: invalid
An invalid character at this point in the scan - reject it.

Constant: terminator
A terminating character at this point in the scan (not part of token).

Data type: ScanState = POINTER TO ScanDesc
ScanState is the type of lexical scanning control procedures. It has a single field of PROCEDURE type:
Field: p: PROCEDURE (ch: CHAR; VAR cl: ScanClass; VAR st: ScanState)

Module IntConv

Module IntConv provides low-level integer/string conversions.

Data type: ConvResults
ConvResults is a local equivalent to ConvTypes.ConvResults. This type has associated constants with the same meanings as in module ConvTypes (see section Module ConvTypes)

Constants strAllRight, strOutOfRange, strWrongFormat, and strEmpty are all valid values for ConvResults.

Procedure: ScanInt (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass; VAR nextState: ConvTypes.ScanState)
Represents the start state of a finite state scanner for signed whole numbers--assigns class of inputCh to chClass and a procedure representing the next state to nextState (see section Module ConvTypes)

Please note: ScanInt is used by procedures FormatInt and ValueInt.

Function: FormatInt (str: ARRAY OF CHAR): ConvResults
Returns the format of the string value for conversion to LONGINT.

Procedure: ValueInt (str: ARRAY OF CHAR): LONGINT
If str is well-formed, returns the value corresponding to the signed whole number represented by the string value str. Otherwise, its behavior is undefined.

Procedure: LengthInt (int: LONGINT): INTEGER
Returns the number of characters in the string representation of int. This value corresponds to the capacity of an array str, which is of the minimum capacity needed to avoid truncation of the result in the call IntStr.IntToStr(int,str) (see section Module IntStr)

Procedure: IsIntConvException (): BOOLEAN
This function returns TRUE if the current process is in the exceptional execution state because of the raising of the IntConv exception; otherwise, it returns FALSE.

Module IntStr

Module IntStr provides integer-number/ string conversions for numbers in the form of signed whole numbers (see section Syntax of Text Tokens).

Data type: ConvResults
ConvResults is a local equivalent to ConvTypes.ConvResults. This type has associated constants with the same meanings as in module ConvTypes (see section Module ConvTypes)

Constants strAllRight, strOutOfRange, strWrongFormat, and strEmpty are all valid values for ConvResults.

Procedure: StrToInt (str: ARRAY OF CHAR; VAR int: LONGINT; VAR res: ConvResults)
This procedure converts a string to an integer value. StrToInt ignores any leading spaces in str. If the subsequent characters in str are in the format of a signed whole number, it assigns a corresponding value to int.

res indicates the result of the conversion based on the format of str.

Example:

VAR stringVar: ARRAY 32 OF CHAR; 
    intVar:    LONGINT;
    res:       IntStr.ConvResults;

stringVar := "   54321"; 
IntStr.StrToInt(stringVar, intVar, res);
   => intVar = 54321, res = strAllRight

stringVar := "12345678901234567890"; 
IntStr.StrToInt(stringVar, intVar, res);
   => intVar is undefined, res = strOutOfRange

stringVar := "54321.0"; 
IntStr.StrToInt(stringVar, intVar, res);
   => intVar is undefined, res = strWrongFormat

stringVar := "   "; 
IntStr.StrToInt(stringVar, intVar, res);
   => intVar is undefined, res = strEmpty

Procedure: IntToStr (int: LONGINT; VAR str: ARRAY OF CHAR)
This procedure converts the value of int to string form and copies the possibly truncated result to str.

Example:

VAR stringVar: ARRAY 6 OF CHAR; 
    intVar:    LONGINT;

intVar := 54321;
IntStr.IntToStr(intVar, stringVar);
   => stringVar = "54321"

intVar := 1234567890;
IntStr.IntToStr(intVar, stringVar);
   => stringVar = "12345"


Go to the first, previous, next, last section, table of contents.