大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> 程序开发 -> Delphi的类型转换

Delphi的类型转换

时间: 2021-07-31 作者:daque

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;

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.