类型
表示范围
字节
ShortInt
-128~127
8位,有正负符号标志
SmallInt
-32 768~32 767
16位,有正负符号标志
LongInt
-2 147 443 648~2 147 483 647
32位,有正负符号标志
Integer
-2 147 443 648~2 147 483 647
32位,有正负符号标志
Int64
-263~263-1
64位,有正负符号标志
Byte
0~255
8位,无正负符号标志
Word
0~65 535
16位,无正负符号标志
LongWord
0~4 294 967 295
32位,无正负符号标志
Cardinal
0~4 294 967 295
32位,无正负符号标志
实数型(Real):实数型是实数的集合,包括多种不同的类型。Real类型只有在和以前Borland Pascal兼容的情况下才使用,否则应使用Double或Extended类型。
| 类型 | 范围 | 有效位 | 存储字节 |
| Real48 | 2.9×10-39 ~ 1.7×1038 | 11~12 | 6 |
| Single | 1.5×10-45 ~ 3.4×1038 | 7~8 | 4 |
| Double | 5.0×10-324 ~ 1.7×10308 | 15~16 | 8 |
| Extended | 3.6×10-4951 ~ 1.1×104932 | 19~20 | 10 |
| Comp | -263+1 ~ 263-1 | 19~20 | 8 |
| Currency | -922 337 203 685 477.5 808 ~ 922 337 203 685 477.5 807 | 19~20 | 8 |
| Real | 5.0×10-324 ~ 1.7×10308 | 15~16 | 8 |
Delphi数据类型分类如下:
| 分类 | 范围 | 字节 | 备注 |
|||
| 简单类型 | 序数 | 整数 | Integer | -2147483648 .. 2147483647 | 4 | 有符号32位 |
| Cardinal | 0 .. 4294967295 | 4 | 无符号32位 |
|||
| Shortint | -128 .. 127 | 1 | 有符号8位 |
|||
| Smallint | -32768 .. 32767 | 2 | 有符号16位 |
|||
| Longint | -2147483648 .. 2147483647 | 4 | 有符号32位 |
|||
| Int64 | -263 .. 263 | 8 | 有符号64位 |
|||
| Byte | 0 .. 255 | 1 | 无符号8位 |
|||
| Word | 0 .. 65535 | 2 | 无符号16位 |
|||
| Longword | 0 .. 4294967295 | 4 | 无符号32位 |
|||
| 字符 | AnsiChar(Char) | ANSI字符集 | 8位 |
|||
| WideChar | Unicode字符集 | 16位 |
||||
| 布尔 | Boolean | False < True | 1 | |||
| ByteBool | False <> True | 1 | ||||
| WordBool | 2 | |||||
| LongBool | 4 | |||||
| 枚举 | ||||||
| 子界 | ||||||
| 实数 |
| Real | 5.0×10-324 .. 1.7×10308 | 8 | [精度]15..16 |
|
| Real48 | 2.9×10-39 .. 1.7×1038 | 6 | [精度]11..12; |
|||
| Single | 1.5×10-45 .. 3.4×1038 | 4 | [精度]7..8 |
|||
| Double | 5.0×10-324 .. 1.7×10308 | 8 | [精度]15..16 |
|||
| Extended | 3.6×10-4951 .. 1.1×104932 | 10 | [精度]19..20 |
|||
| Comp | -263 + 1 .. 263 - 1 | 8 | [精度]19..20 |
|||
| Currency | -922337203685477.5808 .. | 8 | [精度]19..20 |
|||
| 字符串 |
|
| ShortString | 255个字符 | 2..256B | 向后兼容 |
| AnsiString | 大约 231个字符 | 4B..2GB | 8位(ANSI)字符 |
|||
| WideString | 大约 230个字符 | 4B..2GB | 多用户服务和 |
|||
| 其他 | String | |||||
| 结构类型 | 集合 |
| Set | 最多256个元素[0..255] | ||
| 数组 | 静态数组 |
|
| |||
| 动态数组 |
| |||||
| 记录 |
| Record | ||||
| 文件 |
| File | ||||
| 类 |
| Class | ||||
| 类引用 |
| Class reference | ||||
| 接口 |
| Interface | ||||
| 指针类型 | 无类型指针 |
| Pointer | |||
| 有类型指针 | 预定义类型指针 | PAnsiString | ||||
| 过程类型 | 程序过程类型 |
| Procedural | |||
| 对象过程类型 |
| Procedural |
| |||
| 变体类型 |
|
| Variant |
| ||
|
| OleVariant |
| ||||
强制类型转换
一、数的类型转换
把表达式的类型从一种类型转化为另一种类型,结果值是把原始值截断或扩展,符号位保持不变。例如:
数的类型转换
字符转换为整数 Integer('A')
整数转换为字符 Char(48)
整数转换为1个字节的逻辑型 Boolean(0)
整数转换为2个字节的逻辑型 WordBool(0)
整数转换为4个字节的逻辑型 LongBool(0)
整数转换为10进制pascal型字符串 caption:=intToStr(15)
整数转换为16进制pascal型4位字符串 caption:=intToHex(15,4)
地址转换为长整型数 Longint(@Buffer)
二、数的"分开"与"合成"
取32位longint型数的 高16位数为 hiword(longint-var)
低16位数为 loword(longint-var)
取16位数的 高8位数为 hibyte(integer_var)
低8位数为 lobyte(integer_var)
取32位地址的段选择符和偏移量 段选择符(高16位地址)为 selectorof(p)
偏移量(低16位地址)为 offsetof(p)
段选择符和偏移量合成为指针 Ptr(SEG, OFS: Word)相当于C语言的宏MK-FP(SEG,OFS)
例如在Windows中,Task DataBase结构0FAh偏移处包含'TD'标识,我们可以容易地编写如下代码观察到这个位于Windows内部的未公开的秘密:
{函数ptr(seg,ofs)的用法,相当于C语言的MK-FP(seg,ofs)}
var p:pbyte;ch:char;
p:=ptr(getcurrentTask,$FA);
ch:=char(p^); {结果为ch='T'}
p:=ptr(getcurrentTask,$FA+1);
ch:=char(p^); {结果为ch='D'}
三、字符串string 字符数组与指向字符串的指针pchar的区别与联系
1、使用指向字符串的指针,如果不是以0结尾,运行时就会出现错误。为了避免这种错误,需要在字符串结尾人工加入0 即char(0),或用strpcopy函数在字符串结尾自动加0。
例1: 指向字符串的指针,如果不是以0结尾,运行时会出现错误:
{s[0]=3 s[1]='n' s[2]='e' s[3]='w'}
var
s:string;
p:pchar;
begin
s:='new';
label1.caption:=s; {new}
label2.caption:=intTostr(integer(s[0]));{3是字符串的长度}
p:=@s[1];{不是以0结尾,莫用pchar型指针}
label3.caption:=strpas(p); {运行时出现错误}
end;
例2:在字符串结尾人工加入0即char(0),可使用指向字符串的指针。
{s[0]=4 s[1]='n' s[2]='e' s[3]='w' s[4]=0;}
{p-->'new'}
var
s:string;
p:pchar;
begin
p:=@s[1];
s:='new'+char(0); {以0结尾,可用pchar型指针}
label1.caption:=strpas(p); {new}
label2.caption:=s; {new}
label3.caption:=intTostr(integer(s[0])); {4是字符串长度}
end;
例3: 用strpcopy函数赋值会在字符串s结尾自动加0。
{s[0]=4 s[1]='n' s[2]='e' s[3]='w' s[4]=0;}
{p-->'new'}
var
s:string;
p:pchar;
begin
p:=@s[1];
strpcopy(p,'new');{strpcopy函数在字符串结尾自动加0}
label1.caption:=strpas(p);{new}
label2.caption:=s;{new}
label3.caption:=intTostr(integer(s[0]));{4}
end;
2、下标为0的字符串标识符存放的是字符串长度,字符型数组基本相当于字符串,但不能存放字符串长度。字符串可以用s:='a string'的形式赋值,但是字符型数组a[ ]不可直接用a:='array'的形式赋值,用此种形式会出现类型不匹配错误,可选用strpcopy函数赋值。
例4: 字符型数组s[ ]相当于字符串,但没有存放字符串长度的位置。
{s[1]='n' s[2]='e' s[3]='w' s[4]=0;}
{p-->'new'}
var
s:array[1..10] of char;
p:pchar;
begin
{s:='new'+char(0); error}{错误}
p:=@s[1];
{p:=@s; is not correct}
strpcopy(p,'new');
label1.caption:=strpas(p);{new}
label2.caption:=s;{new}
{label3.caption:=intTostr(integer(s[0]));}{不会出现4, 下标超出错误}
end;
例5:下标从0开始的字符数组s,s相当于@s[0]。
{ s[0]='n' s[1]='e' s[2]='w' s[3]=0;}{p-->'new'}
var
s:array[1..10] of char;
p:pchar;
begin
{s:='new'+char(0); error}{错误}
p:=s;
{p:=@s[0] is also correct}
strpcopy(p,'new');
label1.caption:=strpas(p);{new}
label2.caption:=s;{new}
label3.caption:=s[0];{n}
end;
3、下标从0开始和从1开始的字符数组地址的表示方法也有细微不同:
例6:下标从1开始的数组a 与下标从0开始的数组b 的比较。
var
a:array[1..10]of char;
b:array[0..10]of char;
{a:='1..10';}{type mismatch}
{b:='0..10';}{type mismatch}
begin
strpcopy( b, 'from 0 to 10'); {正确 因为b即是@b[0] }
strpcopy(@b[0], 'from 0 to 10'); {正确 与上个表达式结果相同}
strpcopy(@a[1], 'from 1 to 10'); {正确 }
strpcopy( a, 'from 1 to 10'); {类型匹配错误 因为a即是@a[0]}
end;