Delphi is a strong-type conversion language. In VC, the assignment is ″=″, such as X=1, and the Delphi assignment becomes ″:=″, such as X:=1. From the assignment of the symbol ″:=″ instead of ″=″, it is looming the Delphi to type matching requirements of strict, that is, the right type of the assignment must be consistent with the left. Used to VB or VC programmers, the initial use of Delphi, slightly inattentive, there will be a type mismatch error. For beginners, type conversion is also the focus and difficulty of learning Delphi, for this is to Delphi type conversion to do a summary, for the reader's reference.
Type conversion of one or number
Converts the type of an expression from one type to another, resulting in truncation or expansion of the original value, and the symbol bit remains unchanged. For example:
Type conversion of a number
Example
Convert characters to integers
Integer (' A ')
converting integers to Characters
Char (48)
Integer converted to 1-byte logical
Boolean (0)
Integer converted to 2-byte logical
Wordbool (0)
Integer converted to 4-byte logical
Longbool (0)
Convert integer to 10 Pascal string
CAPTION:=INTTOSTR (15)
Integer converted to 16-in Pascal 4-bit string
Caption:=inttohex (15,4)
Address converted to Long integer number
Longint (@Buffer)
"Separation" and "synthesis" of two numbers
The high 16-digit number of the 32-bit longint is
HiWord (Longint-var)
The low 16-digit number is
LoWord (Longint-var)
Take 16-digit high 8-digit number
Hibyte (Integer_var)
The low 8-digit number is
Lobyte (Integer_var)
Take the segment selector and offset segment selector (high 16-bit address) of the 32-bit address as
Selectorof (P)
Offset (low 16-bit address) is
Offsetof (P)
Segment selectors and offsets are synthesized as pointers
Ptr (SEG, Ofs:word) is equivalent to the C-language macro MK-FP (SEG,OFS)
For example, in Windows, where the TASK database structure has a 0FAh offset containing the ' TD ' logo, we can easily write the following code to observe this undisclosed secret inside Windows:
{function ptr (seg,ofs) usage, equivalent to C language MK-FP (SEG,OFS)}
var P:pbyte;ch:char;
P:=ptr (GETCURRENTTASK,$FA);
Ch:=char (p^); {The result is ch= ' T '}
P:=ptr (getcurrenttask,$fa+1);
Ch:=char (p^); {The result is ch= ' D '}
Strings string character arrays and pointing words
The difference and relation of the pointer Pchar of the character string
The basic concepts of these 3 are the same, but there are some very subtle differences that can be error-prone when programming, and require a high degree of attention.
1. Use a pointer to a string, and if it does not end with 0, an error will occur at run time. To avoid this error, you need to manually add 0, char (0) at the end of the string, or use the Strpcopy function to automatically add 0 to the end of the string.
Example 1: A pointer to a string, and if it does not end with 0, an error occurs at run time:
{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 is the length of the string}
P:[email Protected][1]; {Not ending with 0, MO pchar-type pointer}
Label3.caption:=strpas (P); {There was an error at run time}
End
Example 2: Manually adding 0, or char (0) at the end of the string, you can use a pointer to a string.
{s[0]=4 s[1]= ' n ' s[2]= ' e ' s[3]= ' W ' s[4]=0;}
{p--> ' new '}
Var
s:string;
P:pchar;
Begin
P:[email Protected][1];
s:= ' new ' +char (0); {ends with 0, Pchar-type pointers available}
Label1.caption:=strpas (P); {New}
Label2.caption:=s; {New}
Label3.caption:=inttostr (Integer (s[0])); {4 is the string length end;
Example 3: Assigning a value with the Strpcopy function automatically adds 0 to the end of the string S.
{s[0]=4 s[1]= ' n ' s[2]= ' e ' s[3]= ' W ' s[4]=0;}
{p--> ' new '}
Var
s:string;
P:pchar;
Begin
P:[email Protected][1];
Strpcopy (P, ' new '); {strpcopy function automatically adds 0} at end of string
Label1.caption:=strpas (P); {New}
Label2.caption:=s; {New}
Label3.caption:=inttostr (Integer (s[0])); {4}
End
2, the subscript 0 string identifier holds the string length, the character array is basically the equivalent of a string, but cannot hold the string length. The string can be assigned in the form of s:= ' a string ', but the character array a[] cannot be assigned directly in the form of a:= ' array ', in which case a type mismatch error occurs, and the Strpcopy function can be used to assign a value.
Example 4: Character array s[] is equivalent to a string, but there is no position to hold the length of the string.
{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} Error
P:[email Protected][1];
{P:[email protected]; is not correct}
Strpcopy (P, ' new ');
Label1.caption:=strpas (P); {New}
Label2.caption:=s; {New}
{label3.caption:=inttostr (integer (s[0]));} {does not appear 4, the next
Exceeded error}
End
Example 5: Subscript a character array starting from 0 s,s equivalent to @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} Error
P:=s;
{P:[email protected][0] is also correct}
Strpcopy (P, ' new ');
Label1.caption:=strpas (P); {New}
Label2.caption:=s; {New}
Label3.caption:=s[0]; N
End
3, the subscript starting from 0 and starting from 1 the representation of the character array address is also slightly different:
Example 6: A comparison of array B with subscript starting from 1 and a subscript starting at 0.
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 '); {correct because B is @b[0]}
Strpcopy (@b[0], ' from 0 to 10 '); {Correct and previous expression
Same as Fruit}
Strpcopy (@a[1], ' from 1 to 10 '); Correct
Strpcopy (A, ' from 1 to 10 '); {Type Match error because A is a[0]}
End
4. Get the high 16-bit and low 16-bit integers
Use the HiWord method and the LoWord method.
var Intvalue:integer;
Hiw:word;
Low:word;
Begin
hiw:= HiWord (intvalue);
low:= LoWord (intvalue);
intvalue:= (HIW SHL) + low;
End
http://blog.csdn.net/henreash/article/details/3956493
Delphi Type Conversion Good