Delphi data type conversion)

Source: Internet
Author: User
Different classes have different members. Generally, the Child class members add new members after the members of the parent class. If the child class is converted to the parent class, although these new Members still exist

Yes. If the parent class is converted into a subclass, unpredictable errors may occur because the pointer to a Member is not actually a member.
There are also many types of conversions, which are implicit and explicit. Some dedicated type conversion functions will re-create a new type target object, and then move the value of the type.

And then destroy the old object. Some implicit conversions, the object itself does not change, and the pointer type is changed.

@ Is used to obtain the address! The most convenient usage:
When calling an API, for example:
The pchar type is often used when we call an API, while the memory management of the pchar type is a very header.
Pain points. Getmem and freemem must be used first. If @ is used, it is convenient. Declare
String type variable ABC, and then use @ ABC during the call.

The datetimetofiledate function converts the Delphi date format to the DOS date format.
The datetimetostr function converts the datetime format to a string.
The datetimetostring function converts the datetime format to a string.
The datetostr function converts the date format to a string.
The filedatetodatetime function converts the DOS date format to the Delphi date format.
The floattodecimal function converts a floating point number to a decimal number.
The floattostrf function converts a floating point number to a string.
The floattostr function converts a floating point number to a string.
The floattotext function converts a given floating point number to a decimal number.
The floattotextfmt function converts a given floating point number to a decimal number.
Inttohex converts an integer to a hexadecimal number.
Inttostr converts an integer to a string
The stringtowidechar function converts an ANSI string to a unicode string.
The strtodate function converts a string to a date format.
The strtodatetime function converts a string to the date/time format.
The strtofloat function converts a given string to a floating point number.
The strtoint function converts a string to an integer.
The strtointdef function converts a string to an integer or default value.
The strtotime function converts a string to a time format.
The texttofloat function converts a string (in the format ending with null) to a floating point number.
The timetostr function converts the time format to a string.
The vartodatetime function converts a given variant to the datetime type.
The widecharlentostring function converts an ANSI string to a unicode string.

The widechartostring function converts a unicode string to an ANSI string.
The widechartostrvar function converts a unicode string to an ANSI string variable.

Procedure getmem (var p: pointer; Size: integer );

// Allocate dynamic memory

Function strpas (const STR: pchar): string;

// Convert pchar to string

Delphi data type Conversion Function

Computer Knowledge read 100 comments 0

Font size: large, medium, small

CompileProgramMultiple data types are used according to different situations. To operate on different types, you must convert different types to the same type. Therefore, it is very important to master the conversion of data types.

1. floattostr

Function Description: This function is used to convert a floating point type to a floating point type ".

Reference instance:

Edit1.text: = floattostr (1.981 );

2. inttostr

Function Description: This function is used to convert an integer type to an integer type ".

Reference instance:

S: = inttostr (10); (Note: S is a string type variable .)

3. inttohex

Function Description: This function is used to convert "decimal" to "decimal ". This function has two parameters. The first parameter is the decimal data to be converted, and the second parameter is the number of digits used to display hexadecimal data.

Reference instance:

Edit1.text: = inttohex ('000000', 2 );

Execution result. edit1.text is 64.

Note: Delphi does not provide a special function to convert "hexadecimal" to "decimal. Use the strtoint function to implement this function. DetailsCodeYes: I: = strtoint ('s \ ''+ ''64''); then I equals 100. Add a ''s \'' to convert "hexadecimal" to "10 ".

".

4. strtoint

Function Description: This function is used to convert "integer" to "integer ".

Reference instance:

I: = strtoint ('000000 '');

Note: it cannot be converted to a type such as strtoint (''AB'') or strtoint (''haok'') because they do not have a number type.

4.2 strtointdef ('characters', number)

Function Description: returns the string s to be converted to an integer.

If the string is not expressed as an integer, the default value default is returned.

Reference instance:

Spinedit1.value: = strtointdef (edit1.text, 0 );

Note: When edit1.text is converted to a non-integer expression, the default value 0 is returned.

5. strtofloat

Function Description: This function is used to convert "Floating Point" to "floating point ".

Reference instance:

N: = strtofloat (edit1.text );

Note: The content in edit1.text is 1.981 (all texts displayed in the edit control are strings ). N is double type, used to save the converted floating point data.

The datetimetofiledate function converts the Delphi date format to the DOS date format.

The datetimetostr function converts the datetime format to a string.

The datetimetostring function converts the datetime format to a string.

The datetostr function converts the date format to a string.

The filedatetodatetime function converts the DOS date format to the Delphi date format.

The floattodecimal function converts a floating point number to a decimal number.

The floattostrf function converts a floating point number to a string.

The floattostr function converts a floating point number to a string.

The floattotext function converts a given floating point number to a decimal number.

The floattotextfmt function converts a given floating point number to a decimal number.

Inttohex converts an integer to a hexadecimal number.

Inttostr converts an integer to a string

The stringtowidechar function converts an ANSI string to a unicode string.

The strtodate function converts a string to a date format.

The strtodatetime function converts a string to the date/time format.

The strtofloat function converts a given string to a floating point number.

The strtoint function converts a string to an integer.

The strtointdef function converts a string to an integer or default value.

The strtotime function converts a string to a time format.

The texttofloat function converts a string (in the format ending with null) to a floating point number.

The timetostr function converts the time format to a string.

The vartodatetime function converts a given variant to the datetime type.

The widecharlentostring function converts an ANSI string to a unicode string.

The widechartostring function converts a unicode string to an ANSI string.

The widechartostrvar function converts a unicode string to an ANSI string.

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, when the 0fah offset of the task database structure contains the 'td 'identifier, we can easily write the following code to observe the 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 ′}

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); {end with 0, pchar pointer 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. The string can be assigned a value in the form of S: = 'a string ', but the string-type array a [] cannot be assigned a value directly 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 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;

0

0

0

] Keywords, priority rules, type conversion, and so on in Delphi

Trunc converts a real value to the integer value after decimal truncation.
Int returns the integer part of the floating point.
Inttostr converts a value to a string
Inttohex converts a value to a hexadecimal string
Strtoint converts a string to an integer. If the string is not a valid integer, an exception is thrown.
Strtointdef converts a string to an integer. If the string is invalid, a default value is returned.
Val converts a string to a number (the traditional Turbo Pascal routine is used for backward compatibility)
STR converts numbers to formatted strings (traditional Turbo Pascal routines are used for backward compatibility)
Strpas converts a zero-terminated string to a Pascal string. In 32-bit Delphi, this type conversion is automatically performed.
Strpcopy copies a Pascal string to a zero-terminated string. In 32-bit Delphi, this type conversion is automatically performed.
Strplcopy copies part of a Pascal string to a zero-end string.
Floattodecimal converts a floating point number to a decimal floating point record containing an index, number, and symbol.
Floattostr converts a floating point value to a string in the default format
Floattostrf converts a floating point value to a string of a specific format
Floattotext copies a floating point value to a string buffer in a specific format
Floattotextfmt same as the preceding example. A floating point value is copied to a string buffer in a specific format.
Strtofloat converts a Pascal string to a floating point number.
Texttofloat converts a zero-terminated string to a floating-point number.

Dec reduces the value of a parameter in a routine by 1 or a specific value. The specific value can be defined in the second optional parameter.
INC increases the parameter value in the routine by 1 or a specific value.
Odd returns true if the parameter is an odd number
PRED returns the forward value of the parameter value based on the sequence of the parameter in its data type definition.
Succ returns the successor value of the parameter value
Sequence Number of the value returned by Ord in its data type value set
The minimum value of the ordered data type corresponding to the low return Parameter
The maximum value of the ordered data type corresponding to the high return Parameter

Real Number:
Single
Double
Extended
Real {$ realcompatibility on} is said to be not standard and must be compatible with the old format using this macro
Comp
Currency

Tdatetime = type double

To be used later or directly for variables, You need to name the custom type. To customize a named type, you must place the code in a specific type area, as shown below:
The largest number of classes, such as definition classes.
Type
Uppercase = 'A'... 'Z ';
Temperatures = array [1 .. 24] of integer;
Date = record
Month: byte;
Day: byte;
Year: integer;
End;
Colors = (red, yellow, green, cyan, Blue, violet );
Letters = set of char;
End;

Subfield type
A... B
A ~ B is an ordered type.

Enumeration type
Type
Colors = (red, yellow, green); // The attribute list is most frequently enclosed in garden brackets.
End;
Only one value is allowed.
CPP = Enum

Set Type
Type letters = set of char;
VaR letters1, letters2etters;
Begin
Letters1: = ['A'. 'B'. 'C'];
Letters2: = ['K'];
End;
A set of attributes.
CPP = multi-field and or

Font. Style: = [fsbold, fsitalic]; // two styles
Font. Style: = oldstyle + [fsbold, fsitalic];

Array type
Type
Monthtemps = array [1 .. 24, 1 .. 31] of integer;
Yeartemps = array [Jan .. DEC] of integer;

Record type
Type
Date = record
Year: integer;
Month: byte;
Day: byte;
End;
Similar to structure

Pointer
Type
Pointertoint = ^ integer;
VaR
P: ^ integerl
X: integer;
Begin
P: = @ X; P = & X;
X: = 10; X = 10;
P ^: = 20; * P = 20;
End;
In addition to the address indicating the allocated memory, the pointer can also dynamically allocate memory in the heap through the new routine, but when you do not need this pointer, you must also call the dispose routine to release your dynamically allocated memory.
P: = nil;

Pointer type, such as void *;
In fact, pointer must be used in Delphi, which is an attractive advantage of the Delphi development environment.

File Type
Type
Intfile = file of integer;
Can open a physical file corresponding to this structure, write Integers to the file, or read the current value from the file

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.