For beginners, this section can be skipped temporarily, but you will have to pay it back sooner or later. After learning for a while, you will find that the data type is very important, especially when you analyze the Delphi source code
Long text warning...
There are many data types that can be defined in Delphi. Today I will talk about the simpler types that are easier, including: ordered data types, real number types and string types. The string type is quite special, wait for a separate article to explain later
classification | range | byte | Remarks | |||
Simple type | Ordinal | Integer | Integer | -2147483648 .. 2147483647 | 4 | Signed 32-bit |
Cardinal | 0 .. 4294967295 | 4 | Unsigned 32-bit | |||
Shortint | -128 .. 127 | 1 | Signed 8-bit | |||
Smallint | -32768 .. 32767 | 2 | Signed 16-bit | |||
Longint | -2147483648 .. 2147483647 | 4 | Signed 32-bit | |||
Int64 | -263 .. 263 | 8 | Signed 64-bit | |||
Byte | 0 .. 255 | 1 | Unsigned 8-bit | |||
Word | 0 .. 65535 | 2 | Unsigned 16-bit | |||
Longword | 0 .. 4294967295 | 4 | Unsigned 32-bit | |||
character | AnsiChar (Char) | ANSI character set | 8th place | |||
WideChar | Unicode character set | 16th place | ||||
Boolean | Boolean | False < True Ord(False) = 0 Ord(True) = 1 Succ(False) = True Pred(True) = False |
1 | |||
ByteBool | False <> True Ord(False) = 0 Ord(True) <> 0 Succ(False) = True Pred(False) = True |
1 | ||||
WordBool | 2 | |||||
LongBool | 4 | |||||
enumerate | ||||||
Child world | ||||||
Real number | Real | 5.0×10-324 .. 1.7×10308 | 8 | [Accuracy] 15..16 | ||
Real48 | 2.9×10-39 .. 1.7×1038 | 6 | [Accuracy] 11...12; backward compatible |
|||
Single | 1.5×10-45 .. 3.4×1038 | 4 | [Accuracy] 7..8 | |||
Double | 5.0×10-324 .. 1.7×10308 | 8 | [Accuracy] 15..16 | |||
Extended | 3.6×10-4951 .. 1.1×104932 | 10 | [Accuracy] 19..20 | |||
Comp | -263 + 1 .. 263 - 1 | 8 | [Accuracy] 19..20 | |||
Currency | -922337203685477.5808 .. 922337203685477.5807 |
8 | [Accuracy] 19..20 |
Signed and unsigned
It is easy to notice these two words in the above table. When I first came into contact with Delphi, I was puzzled because there is no such concept in Java. It seems that only in C, C++ and Delphi, especially Win32API, there are a lot of such statements. If there are words in other languages, I can only apologize, I can only say that I have not contacted...
Signed numbers (signed) can represent integers (including negative numbers) within the specified range of a specific type, while unsigned numbers can only represent non-negative numbers (0 and positive numbers) .
在数学中,任意基数的负数都在最前面加上"−"符号来表示。然而在计算机硬件中,数字都以无符号的二进制形式表示,因此需要一种编码负号的方法。当前有四种方法,用于扩展二进制数字系统,来表示有符号数:原码(sign-and-magnitude)、反码(ones’ complement)、补码(two’s complement)以及移码(offset binary,excess-N)
以上内容摘自维基百科对于有符号数的处理论述
地址:https://zh.wikipedia.org/wiki/%E6%9C%89%E7%AC%A6%E8%99%9F%E6%95%B8%E8%99%95%E7%90%86
上面说到了关于有符号数的表示方法(即:原码、反码、补码以及移码),我梳理了一下相关内容,以便更容易理解
(1)原码: 一个整数,按照绝对值大小转换成的二进制数,最高为为符号位,称为原码。 加粗为符号位
例如:X=-5
正:0 0000000 00000000 00000000 00000101
负:1 0000000 00000000 00000000 00000101
(2)反码: 将二进制除符号位数按位取反,所得的新二进制数称为原二进制数的反码。 正数的反码为原码,负数的反码是除原码符号位外按位取反,取反操作指:原为1,变0;原为0,变1。
例如:X=-5
原:10000000 00000000 00000000 00000101
反:11111111 11111111 11111111 11111010
即 11111111 11111111 11111111 11111010 为 10000000 00000000 00000000 00000101 的反码。
(3)补码: 反码加1称为补码。 正数的补码和原码相同。负数要得到一个数的补码,先得到反码,然后将反码加上1,所得数称为补码。
例如:X=-5
原:11111111 11111111 11111111 11111010
反:10000000 00000000 00000000 00000101
补:11111111 11111111 11111111 11111011
(4)移码: 实际上就是改变了补码的符号位,从0变1,从1变0。
例如:X=-5
原=1000000 00000000 00000000 00000101
反=1111111 11111111 11111111 11111010
补=1111111 11111111 11111111 11111011
移=0111111 11111111 11111111 11111010
有序类型
有序类型是建立在概念“顺序”或“序列”基础上的数据类型。你不仅可比较两个有序值的大小,而且可以求取给定有序值的前驱及后继,或者计算它们的最大或最小值。
布尔类型
布尔值不同于布尔类型,平时很少用到。ByteBool、 WordBool 和LongBool这三种布尔类型的布尔值比较特殊,只在Windows API 函数中才用到它们。
在Delphi 3 中,为了与Visual Basic 和 OLE Automation兼容,修改了ByteBool、 WordBool 和LongBool的布尔值,将TRUE值设置为1,FALSE值仍为0;Boolean类型布尔值保持不变(TRUE为1,FALSE为0)。如果在Delphi 2代码中使用了布尔值显式类型转换 ,那么在以后的Delphi中可能会出错。
实数类型
实数类型(其实也就是我们平时说的小数点)代表不同格式的浮点数。Single类型占的字节数最小,为4个字节;其次是Double 浮点类型,占8个字节;Extended 浮点类型,占10个字节。
这些不同精度的浮点数据类型都与IEEE( 电气和电子工程师协会)标准的浮点数表示法一致,并且 CPU数字协处理器直接支持这些类型,处理速度也最快
需要注意的是Real 类型在Delphi 2 和 Delphi 3 中的定义与 16 位版本一样,都占 6 个字节。不过Borland公司一直不提倡使用这种类型,而建议用Single、 Double、 Extended 类型代替
参考资料:https://www.tcoline.com/resource/Delphi/delphi_datatype.htm
类型转换
其实我认为的类型转换一直都是有两种形式一种是自动类型转换,另一种为强制类型转换
自动类型转换:在有序类型的范围内类型转换的一种形式,即字节数小的向字节数大的类型内进行转换,编译器可以自动完成类型转换
procedure TForm1.Button1Click(Sender: TObject);
var
Num:Double;
begin
Num:=1;
end;
强制类型转:即上面自动转换反过来的玩儿法,字节数大的向字节数小的类型内进行转换,而这种转换编译器是不会帮我们完成,需要我们显示的进行处理
具体的实现方式也有两种,第一种为类型映射(书上是这么称呼的),第二种是利用Delphi的一些内置函数完成
例如:假定要把一个字符类型的值赋给一个byte类型的变量:
var
c: char;
b: byte;
begin
c:= 'a';
b:= c; //编译器要提示错误
end.
在下面的代码中,强制类型转换把c转换成byte类型,事实上强制类型转换是告诉编译器你知道你正在做什么,并要把一种类型转换为另一种类型:
var
c: char;
b: byte;
begin
c:= 's';
b:= byte(c); //编译器不会报错
end.
如果以字符转字节的方式无法理解的话,那看下面的例子
procedure TForm1.Button1Click(Sender: TObject);
var
Num:Double;
Num2:Integer;
begin
Num2:=Integer(Num2);//编译器不会报错
end;
使用Delphi内置的函数实现类型转换的方式比较简单,下面是常用函数的一张表
例程 | 作用 |
---|---|
Chr | 将一个有序数据转换为一个ANSI字符 |
Ord | 将一个有序类型值转换为它的序号 |
Round | 转换一个实型值为四舍五入后的整型值 |
Trunc | 转换一个实型值为小数截断后的整型值 |
Int | 返回浮点数的整数部分 |
IntToStr | 将数值转换为字符串 |
IntToHex | 将数值转换为十六进制数字符串 |
StrToInt | 将字符串转换为一个整型数,如字符串不是一个合法的整型将引发异常 |
StrToIntDef | 将字符串转换为一个整数,如字符串不合法返回一个缺省值 |
Val | 将字符串转换为一个数字(传统Turbo Pascal例程用于向后兼容) |
Str | 将数字转换为格式化字符串(传统Turbo Pascal例程用于向后兼容) |
StrPas | 将零终止字符串转换为Pascal类型字符串,在32位Delphi中这种类型转换是自动进行的 |
StrPCopy | 拷贝一个Pascal类型字符串到一个零终止字符串, 在32位Delphi中这种类型转换是自动进行的 |
StrPLCopy | 拷贝Pascal类型字符串的一部分到一个零终止字符串 |
FloatToDecimal | 将一个浮点数转换为包含指数、数字及符号的十进制浮点记录类型 |
FloatToStr | 将浮点值转换为缺省格式的字符串 |
FloatToStrF | 将浮点值转换为特定格式的字符串 |
FloatToText | 使用特定格式,将一个浮点值拷贝到一个字符串缓冲区 |
FloatToTextFmt | 同上面例程,使用特定格式,将一个浮点值拷贝到一个字符串缓冲区 |
StrToFloat | 将一个Pascal字符串转换为浮点数 |
TextToFloat | 将一个零终止字符串转换为浮点数 |