Delphi type conversion

Source: Internet
Author: User
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

Convert character to integer
INTEGER ('A ')

Integer to character
Char (48)

Converts an integer to a logic type of 1 byte.
Boolean (0)

Converts an integer to a logic type of 2 bytes.
Wordbool (0)

Converts an integer to a logic type of 4 bytes.
Longbool (0)

Converts an integer to a 10-digit Pascal string.
Caption: = inttostr (15)

Converts an integer to a hexadecimal Pascal-type 4-Bit String.
Caption: = inttohex (15, 4)

Converts an address to a 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 low is
Loword (longint-var)

16-digit high 8-digit
Hibyte (integer_var)

Low 8-digit
Lobyte (integer_var)

Take the 32-bit segment selection character and offset segment selection character (high 16-bit address) is
Selectorof (P)

The offset (16-bit low) is
Offsetof (P)

Segment selection operator and offset synthesis as pointer
PTR (SEG, OFS: Word) is equivalent to the macro MK-FP (SEG, OFS) of C Language)

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}
Label2.caption: = s; {New}
{Label3.caption: = inttostr (INTEGER (s [0]);} {No 4 appears, lower
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'); {correct match with the previous expression
Same result}
Strpcopy (@ A [1], 'From 1 to 10'); {correct}
Strpcopy (A, 'From 1 to 10'); {type matching error because a is a [0]}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.