Last Updated:

Delphi symbols

Let's talk about how Delphi characters represent and what type of data they can be. To store and process symbols, use variables that have the Ansichar and wideChar data type:

  1. The Ansichar data type is represented as a set of ANSI characters that contain characters encoded by a single byte (a byte is an eight-bit binary number).
  2. The wideChar data type corresponds to a Unicode character set that includes characters encoded by two bytes.

To ensure compatibility with previous versions, use the Char data type, which is equivalent to the Ansichar type. The value of a variable of a character type can be any displayed symbol:

  • any figure,
  • any punctuation mark,
  • any letter of the Russian or Latin alphabet,

and a special character, such as 'new line'. As expected, a variable with a character data type is declared in the var section (the variable declaration section). The general form of the Delphi character declaration statement is as follows:

 

name: char;

 

From where

  • name – means a variable with a character data type;
  • char is a special keyword that denotes a character type.

Example 1.

 

st: char; 

 

family: char;

 

As with any variable, a variable of the char character type acquires value during the execution of the assignment instruction. For example, if, after an assignment instruction is executed, a variable of type char receives some value, then to the right of the assignment sign := should be an expression with a type char, for example, a variable with a type char, or a character constant - some character enclosed in quotation marks.

Example 2. Let the variables a and b be variables of character type. As soon as the instructions are followed

 

n:= ‘*’; 

 

m:=a;

 

the variable a acquires value by assigning a value to the specified constant, and the variable b by assigning a value to the above-mentioned variable a. Comparing a variable with a character type char with some other variable of type char, or with a character constant, is also possible.

This comparison is that each character corresponds to a certain number. For example, the symbol 'o' is a smaller number than the symbol Y, the symbol 'A' is a smaller number than the symbol 'c', the symbol V is mapped to a number less than a. As a result, you can write:

 

‘0’ < ‘1’ < ... < ‘9’ < ... < ‘A’ < ‘B ‘< ... < ‘Z’ < ‘a’ < ‘b’ < ... < ‘z’

 

Large numbers are put in correspondence to the symbols of the Russian alphabet, while small numbers denote the symbols of the Latin alphabet. The following statement is true:

 

'A' < 'B' < 'C' < ... < 'U' < 'I' < 'a' < 'b' < 'c' < ... < 'e' < 'yu' < 'I'

 

In the source code of the program, instead of the symbol itself, you can specify the corresponding code, consisting of the # operator and a certain number following it. For example, instead of the 'in' symbol, you can specify its code:#193.

This method of representing a symbol is best used when you need to write down service characters or symbols that cannot be entered from the keyboard during the compilation of the program. For example, very often in the source code of the program in the process of writing messages together with the character "new line" the code #13 is used. To process Delphi characters, use the chr and ord functions.

Chr Delphi function

The chr function returns a value, which is a character whose code is a parameter of this function.

Example

 

n:=chr(32);

 

This statement means that the variable n is set to space.

Ord Delphi function

The ord function defines a character code that is passed to it as a parameter.

Example

 

m:=ord(‘*’);

 

The result of this statement is the variable m, which contains the number 42, which is the code of the symbol "*".

Examples of programs on Delphi characters

Let's create a program (its listing is presented below), which displays the letters of the Russian alphabet and their encoding:

As we can see from the source code of the program, the main work is done using the OnActivate event handling procedure, which generates and displays the necessary table in the label field (in our case, Label1). The OnActivate event is triggered when an application form is invoked. As a result, as soon as the form appears on the display, the TForm1.FormActivate procedure is automatically executed. In Fig. below is the view of the window of our program:

 

The following illustration shows the Character Table application window, and you can see that there is only one Component label1 on it. To ensure the same width for the columns of the table, you must assign the Label1.Font.Name property a font name that represents all characters with the same width, for example, the Courier New Cyr font.