类型强制和转换

I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
类型强制,不是类型转换哦,Delphi的帮助主题:typecasts!
Delphi的类型转换
关键词:Delphi控件杂项
  Delphi是一种强类型转换的语言。在VC中,赋值符用″=″,例如x=1;到了Delphi赋值符就变成了″:=″,例如x:=1。 从赋值时用符号″:=″而不用″=″,就隐约可见Delphi对类型匹配要求之严,即赋值符右边的类型一定要和左边一致。用惯了VB或VC的程序员,初用Delphi,稍不留神,就会出现类型不匹配的错误。对初学者而言,类型转换也是学习Delphi的重点和难点,为此本文特对Delphi的类型转换做一总结,以供读者参考。
一、数的类型转换
  把表达式的类型从一种类型转化为另一种类型,结果值是把原始值截断或扩展,符号位保持不变。例如:
数的类型转换 举例
字符转换为整数 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的区别与联系
  这3者的基本概念相同,但有一些非常细微的差别,在编程时稍不注意就会出错,需高度重视。
  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;
 
顶部