0% found this document useful (0 votes)
17 views

Extra 01

Uploaded by

skamelrech2020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Extra 01

Uploaded by

skamelrech2020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

60 - 02: Variables and Data Types

What you can do (and it is highly suggested to do) is to turn on a compiler option
called “Overflow checking”, which will guard against a similar overflow operation
and raise an error, in this specific case “Integer overflow”. I've enabled this check in
the IntegersTest demo, so you'll see an error message when you run it.

Boolean
Logical True and False values are represented using the Boolean type. This is also
the type of the condition in conditional statements, as we'll see in the next chapter.
The Boolean type can only have one of the two possible values True and False.

note For compatibility with Microsoft's COM and OLE automation, the data types ByteBool, Word-
Bool, and LongBool represent the value True with -1, while the value False is still 0. Again, you
should generally ignore these types and avoid all low-level Boolean manipulation and numeric
mapping unless absolutely necessary.

Unlike in the C language and some of its derived languages, Boolean is an enumer-
ated type in Object Pascal, there is no direct conversion to the value representing the
Boolean, and you should not abuse direct type casts by trying to convert a Boolean to
a numeric value. It is true, however, that Boolean type helpers include the functions
ToInteger and ToString. I cover enumerated types later in this chapter.
Notice that using ToString returns the string with the numeric value of the Boolean
variable. As an alternative you can use the BoolToStr global function, setting the
second parameter to True, to indicate the use of Boolean strings ('True' and 'False')
for the output. (See the section “Char Type Operations” below for an example.)

Characters
Character variable are defined using the Char type. Unlike older versions, the lan-
guage today uses the Char type to represent double-byte Unicode characters.

note The Windows and Mac version of the compiler still offer the distinction between AnsiChar for one
byte ANSI characters and WideChar for Unicode ones, with the Char type defined as an alias of the
latter. The recommendation is to focus on WideChar, and use the Byte data type for single byte
elements.

For an introduction to characters in Unicode, including the definition of a code point


and that of surrogate pairs (among other advanced topics) you can read Chapter 6.
In this section I'll just focus on the core concepts of the Char type.

Marco Cantù, Object Pascal Handbook


02: Variables and Data Types - 61

As I mentioned earlier while covering literal values, constant characters can be rep-
resented with their symbolic notation, as in 'k', or with a numeric notation, as in
#78. The latter can also be expressed using the Chr system function, as in Chr (78).
The opposite conversion can be done with the Ord function. It is generally better to
use the symbolic notation when indicating letters, digits, or symbols.
When referring to special characters, like the control characters below #32, you'll
generally use the numeric notation. The following list includes some of the most
commonly used special characters:
#8 backspace
#9 tab
#10 newline
#13 carriage return
#27 escape

Char Type Operations


As other ordinal types, the Char type has several predefined operations you can
apply to variables of this type using the dot notation. These operations are defined
with an intrinsic record helper, again.
However, the usage scenario is quite different. First, to use this feature you need to
enable it by referring to the Character unit in a uses statement. Second, rather than
a few conversion functions, the helper for the Char type includes a couple of dozen
Unicode-specific operations, including tests like IsLetter, IsNumber, and IsPunctu-
ation, and conversions like ToUpper and ToLower. Here is an example taken from
the CharsTest application project:
uses
Character;
...
var
ch: Char;
begin
ch := 'a';
Show (BoolToStr(ch.IsLetter, True));
Show (ch.ToUpper);
The output of this code is:
True
A

note The ToUpper operation of the Char type helper is fully Unicode enabled. This means that if you
pass an accented letter like ù the result will be Ù. Some of the traditional RTL functions are not so
smart and work only for plain ASCII characters.

Marco Cantù, Object Pascal Handbook


62 - 02: Variables and Data Types

Char as an Ordinal Type


The Char data type is quite large, but it is still an ordinal type, so you can use Inc
and Dec functions on it (to get to the next or previous character or move ahead by a
given number of elements, as we have seen in the section “Standard Ordinal Types
Routines”) and write for loops with a Char counter (more on for loops in the next
chapter).
Here is a simple fragment used to display a few characters, obtained by increasing
the value from a starting point:
var
ch: Char;
str1: string;
begin
ch := 'a';
Show (ch);
Inc (ch, 100);
Show (ch);

str1 := '';
for ch := #32 to #1024 do
str1 := str1 + ch;
Show (str1)
The for loop of the CharsTest application project adds a lot of text to the string,
making the output is quite long. It starts with the following lines of text:
a
Å
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abc
defghijklmnopqrstuvwxyz{|}~
// few more lines omitted...

Converting with Chr


We have seen that there is an Ord function that returns the numeric value (or Uni-
code code point) of a character. There is also an opposite function you can to get the
character corresponding to a code point, that is the Chr special function.

32-bit Characters
Although the default Char type is now mapped to WideChar, it is worth noticing that
Delphi also defines a 4-byte character type, UCS4Char, defined in the System unit as:
type
UCS4Char = type LongWord;

Marco Cantù, Object Pascal Handbook

You might also like