Delphi type conversion

xiaoxiao2021-03-06  19

Delphi is a strong language. In the VC, the assignment is "=", for example, X = 1; to the Delphi assignment, ": =", such as x: = 1. From the assignment, use the symbol ": =" without "=", it can be seen that Delphi is strict on the type matching requirements, that is, the type of assassifier must be consistent with the left. Using programmers who are used to VB or VC, using Delphi, if they don't pay attention, the type does not match the error. For beginners, type conversion is also the focus and difficulties of learning Delphi, and this paper makes a summary for the reader's reference for this.

First, the type conversion transitions the type of expression from one type to another, and the result value is truncated or expanded, and the symbol bit remains unchanged. For example: Type conversion example

Characters Convert to Integer ('A')

Integer converted to character char (48)

Integer converted to 1 byte logical Boolean (0)

Integer converted to 2 bytes of logical wordbool (0)

Integer converted to 4 bytes logical longbool (0)

Integer converted to 10 enrollment PASCAL string CAPTION: = INTTOSTR (15)

Integers converted to 16 credit PASCAL 4-bit string CAPTION: = INTTOHEX (15, 4)

Address Convert to Long Integer Longint (@Buffer)

Second, the number of "separate" and "synthetic"

Take 32-bit longint type high 16 digits for HiWord (longint-var)

Low 16 digits is loword (longint-var)

Take 16 digits of high 8-digit Hibyte (Integer_var)

Low 8 digits is Lobyte (Integer_var)

Take a 32-bit address segment selector and offset segment selector (high 16-bit address) for Selectorof (P)

Offset (low 16-bit address) is OFFSTOF (P)

Segment selectors and offset are synthesized to pointers PTR (SEG, OFS: WORD) equivalent to C language macro MK-FP (SEG, OFS)

For example, in Windows, the Task Database Structure The 0FAH offset contains the 'TD' identifier, and we can easily write the following code to observe that unobilized secrets within 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 ^); {result is CH = 't'}

P: = PTR (GetCurrentTask, $ FA 1);

CH: = char (p ^); {Result is CH = 'd'}

Third, the string String character array is the same as the difference between PCHAR in the string. The basic concept of PCHAR is the same, but there are some very little differences. It will be wrong when programming, and it is necessary to pay attention to it. 1. Use the pointer to the string, if it is not ending with 0, there is an error when running. To avoid such errors, it is necessary to artificially add 0, CHAR (0), or use the strpcopy function to automatically add 0 at the end of the string. Example 1: Pointer to the string, if not ending at 0, error occurs when running: {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: = INTOSTR (Integer (S [0])); {3 is character String length} P: = @ s [1]; {is not ending with 0, do not use PCHAR type pointer} label3.caption: = strPas (p); {Running error} END;

Example 2: Manual join 0 is CHAR (0) at the end of the string, can use the pointer to the 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: = @ s [1]; s: = 'new' char (0); {End of 0, available PCHAR type pointer} label1.caption: = strPas (p); { New} label2.caption: = s; {new} label3.caption: = INTOSTR (Integer (S [0])); {4 is a string length end;

Example 3: Assign a value with the strpcopy function to automatically add 0 at 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: = @ s [1]; strpcopy (p, 'new'); {strpcopy function is automatically adding 0} label1.caption: = strpas (p); {new} Label2.caption: = s; {new} label3.caption: = INTOSTR (Integer (S [0])); {4} end;

2, the string identifier of the subscript 0 is the string length, and the character type array is basically equivalent to the string, but the string length cannot be stored. The string can be assigned in the form of s: = 'a string', but the character type array a [] cannot be assigned directly with A: = 'array', using this form, the type does not match the error, and the STRPCOPY function can be selected. Value.

Example 4: Character-type array s [] is equivalent to a string, but does not store the length of the string length.

{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: = @ s [1]; {p: = @ s; is not correlectr} strpcopy p, 'new'); label1.caption: = strPas (p); {new} label2.caption: = s; {new} {label3.caption: = INTOSTR (Integer (s [0]));} { 4, lower error} END;

Example 5: The subscript starts from 0 0 arrays S, S is 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: = @ s [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 starts from 0 And the representation of the character array address starting from 1 is also different:

Example 6: Comparison of the array A and the subscript start from 0 from 1 to the array b starting from 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 The same as the previous expression} strpcopy (@A [1], 'from 1 to 10'); {correct} strpcopy (A, 'from 1 to 10'); {type matching error Because A is A [0 END;

转载请注明原文地址:https://www.9cbs.com/read-42312.html

New Post(0)