The fundamental character types
are AnsiChar and WideChar. AnsiChar values are byte-sized
(8-bit) characters ordered according to the locale character set which is
possibly multibyte. AnsiChar was originally modeled after the ANSI
character set (thus its name) but has now been broadened to refer to the
current locale character set.
WideChar characters use more than one byte to represent every character. In
the current implementations, WideChar is word-sized (16-bit) characters
ordered according to the Unicode character set (note that it could be longer in
future implementations). The first 256 Unicode characters correspond to the
ANSI characters.
The generic character type is Char,
which is equivalent to AnsiChar. Because the implementation of Char
is subject to change, it’s a good idea to use the standard function SizeOf
rather than a hard-coded constant when writing programs that may need to handle
characters of different sizes.
A string constant of length 1,
such as 'A', can denote a character value. The predefined function Chr
returns the character value for any integer in the range of AnsiChar or WideChar;
for example, Chr(65) returns the letter A.
Character values, like integers,
wrap around when decremented or incremented past the beginning or end of their
range (unless range-checking is enabled). For example, after execution of the
code
var
Letter: Char;
I: Integer;
begin
Letter := High(Letter);
for I := 1 to 66 do
Inc(Letter);
end;
Letter has the value A (ASCII 65).
Working with null-terminated strings
基本字符类型是AnsiChar和WideChar。AnsiChar的尺寸是一个字节(8位),其值参照本地字符集(尽管该字符集可能是多字节的)。AnsiChar最初模仿ANSI字符集(名称来源),但现在用于扩大到是指当前的本地字符集。
WideChar字符用多于一个字节来表示每个字符。在当前Object Pascal实现中,WideChar的尺寸是一个字(16位),其字符顺序参照Unicode字符集(主意,在将来的实现中可能会加长)。Unicode字符集中的前256个字符对应ANSI字符。
一般字符类型是Char,等价于AnsiChar。因为Char的实现可能随着Object Pascal的更新而发生改变,因此,在编程时需要处理不同尺寸的字符时,使用标准函数SizeOf要比直接使用硬编码常量可靠。
长度为1的串常量,如
'A',可以表示字符的值。预定义函数Chr返回对任何在AnsiChar或WideChar范围内的整数返回一个相应的字符值,如Chr(65)返回字母A。
和整数值一样,当对其范围的起点递减或终点递增时,也可以视为一个环(当范围检查编译指示{$R+}有效时除外)。例如,执行下面的代码:
var
Letter: Char;
I: Integer;
begin
Letter := High(Letter);
for I := 1 to 66 do
Inc(Letter);
end;
之后,字符Letter的值为 A (ASCII值为65)。