|
Delphi is a language for strong type conversion. In VC, the value assignment character is "=", for example, x = 1; when the Delphi value assignment character is ": =", for example, X: = 1. Using the symbol ": =" instead of "=" when assigning values, you can vaguely see That Delphi has strict requirements on type matching, that is, the type on the right of the value assignment must be consistent with that on the left. Programmers who are familiar with VB or VC may encounter Type Mismatch Errors when using Delphi. For beginners, type conversion is also the focus and difficulty of learning Delphi. This article summarizes the type conversion of Delphi for your reference. I. Data Type Conversion Convert the expression type from one type to another. The result value truncates or extends the original value, and the symbol bit remains unchanged. For example: Data type conversion example Character to integer INTEGER ('A ') Integer to Char (48) Integer to 1-byte logical Boolean (0) The integer is converted to a logical wordbool (0) of 2 bytes) Integer to 4-byte logical longbool (0) Convert an integer to a 10-digit Pascal string caption: = inttostr (15) Integer to hexadecimal Pascal 4-Bit String caption: = inttohex) Address to long integer longint (@ buffer) 2. "separation" and "synthesis" of numbers" The 16-digit high value of the 32-bit longint type is hiword (longint_var) The 16-digit loword (longint_var) 16-digit high 8-digit hibyte (integer_var) Low 8-digit lobyte (integer_var) Select the 32-bit segment and the offset segment as the selectorof (P) The offset (16-bit low) is offsetof (P) Segment selection character and offset synthesis as pointer PTR (SEG, OFS: Word) equivalent to the macro MK-FP of C language (SEG, OFS) For example, in windows, the 0fah offset of the task database structure contains the 'td 'identifier. We can easily write the following code to observe the undisclosed secret in 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 '}3. String string character array and pointing word Difference and connection between pointer pchar of string These three have the same basic concepts, but there are some very subtle differences. errors may occur if you do not pay attention to programming. 1. Use a pointer to a string. If it does not end with 0, an error will occur during running. To avoid this error, manually add 0 to the end of the string, that is, char (0), or use the strpcopy function to automatically add 0 to the end of the string. Example 1: pointer to a string. If it does not end with 0, an error occurs during 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: = inttostr (INTEGER (s [0]); {3 is the length of the string} P: = @ s [1]; {not end with 0, pchar pointer is not used} Label3.caption: = strpas (p); {An error occurred while running} End;Example 2: manually add 0 to the end of a string, that is, char (0). 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: = @ s [1]; S: = 'new' + char (0); {ends with 0. pchar pointers are available} Label1.caption: = strpas (p); {New} Label2.caption: = s; {New} Label3.caption: = inttostr (INTEGER (s [0]); {4 is the string length} End;Example 3: The strpcopy function automatically adds 0 to the end of 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'); {The strpcopy function automatically adds 0 to the end of the string} Label1.caption: = strpas (p); {New} Label2.caption: = s; {New} Label3.caption: = inttostr (INTEGER (s [0]); {4} End;2. the string identifier with a subscript of 0 stores the string length. The character array is basically equivalent to a string, but cannot store the string length. A string can be assigned a value in the form of S: = 'a string ', but a [] of the string type cannot be assigned a value in the form of a: = 'array, in this form, a Type Mismatch Error occurs. You can use the strpcopy function to assign values. Example 4: String-type array s [] is equivalent to a string but does not store 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 correct} Strpcopy (p, 'new '); Label1.caption: = strpas (p); {New} Abel2.caption: = s; {New} {Label3.caption: = inttostr (INTEGER (s [0]) ;}{ No 4 error will occur, subscript exceeding error} End;Example 5: The character array s whose subscript starts from 0, and 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 representation of the character array addresses starting from 0 and starting from 1 is also slightly different: Example 6: Compare array a whose subscript starts from 1 with array B whose subscript starts 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'); {the correct result is the same as that of 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; |