Previous Table of Contents Next


5.2.6.3. Prewritten Units

Turbo Pascal’s prewritten functions and procedures come in different units. The more uncommon ones are seen, more than likely, in the \units directory of your Turbo Pascal installation. The more common ones are loaded into memory from a “packed” file that exists in the bin directory when the compiler is evoked. Some of these common units include the CRT unit, which holds items that involve screen control and output in a text-oriented way; the DOS unit, which holds items that involve implementation of DOS-related functions; and the system unit (which need not be called in the uses clause), which contains elementary procedures and functions. write, writeln, read, and readln do not need a uses clause because they are created in the system unit.

5.2.7. Data Manipulation Procedures and Functions

This section describes some of the more commonly used functions and procedures available to perform manipulation on a data type, such as typecasting, conversions, and modifications, via mathematical evaluation or otherwise. The pseudo-random number generator and bitwise operations (or “bit fiddling”) are also covered.

5.2.7.1. Typecasting and Type Conversions

Typecasting and type conversions on data are described in this section. All functions and procedures involved come from the system unit.

5.2.7.2. Typecasting

Typecasting is exactly what it implies: the interpretation of one type into another type. Generally, it is done with the typical variable types that are defined, with the type name used as a function. It is done in a function style, where the interpretation of the variable is changed, and not its format in memory. An example of typecasting (and one case in which it might be used) is shown in Listing 5.23.

Listing 5.23. A demonstration of typecasting.

  program fig23;
    { demonstrates type casting and what can be done }
    var
      a: char;
      b: byte;
    begin
      a := ‘C’;
      b := 45;
      writeln(‘a is: ’, a);
      writeln(‘b is: ’, b);
      writeln(‘b char: ’, char(b));
      writeln(‘a byte: ’, byte(a));
      writeln(‘a byte + b: ’, byte(a) + b);
      writeln(‘a + b char: ’, a + char(b));
    end.

ord() and chr()

These two functions are most commonly used to convert characters between their ASCII equivalent character and the byte that is stored:

   ord(x: ordinal type): longint;
   chr(x: longint): char;

For example, ord(‘A’) returns 65, and chr(65) returns ‘A’. An example of use is shown in Listing 5.24.

Listing 5.24. An example of the use of ord() and chr().

  program fig24;

    { a demonstration of ord() and chr() to prove that ‘A’ is a byte by
    the value of 65 }

    begin
      if ord(‘A’) = 65 then
        writeln(‘A character ”A” is stored as a byte = 65 according ’,
                ‘to ord’);
      if chr(65) = ‘A’ then
        writeln(‘A byte = 65 is stored as a character ’‘A’ ‘ according ’,
                ‘to chr’);
    end.

str and val

These procedures are used to convert a number variable to a string, and a numerically oriented string to a number variable, in that order:

  str(x: <a number>:<optional formatting as in writes>; var s: string);
  val(s: string; x: <a number>; errorcode: integer);

str() takes a number, with desired formatting codes if it is a real number (if you want to have it as a decimal and not as a scientific number), like writing reals in write or writeln, and then the string the variable is going to end up in.

val() takes a string, which should have only numeric characters, as well as periods, and a numeric variable, as well as an integer-related error variable. It places the numeric equivalent of s into the variable x. errorcode is not 0, denoting the position of error if it is non-numeric, if the string s was not convertible. This variable must be checked after each and every call of val.

Demonstrations of these procedures are shown in Listing 5.25.

Listing 5.25. A demonstration of val and str.

  program fig25;

    { demonstrates and writes results of the use of val and str }
    var
      entry: string;
      number: real;
      error: integer;
    begin
      write(‘Enter something: ’);
      readln(entry);
      val(entry, number, error);
      if error <> 0 then { show error at position }
        writeln(‘Non-numeric character found at position ’, error)
      else
        writeln(number);
      number := 5.23342;
      str(number:0:2, entry); { format string may be used here}
      writeln(entry);
    end.


Previous Table of Contents Next