Delphi type conversion

Source: Internet
Author: User
Tags natural logarithm types of functions

Chr converts an ordered data to an ANSI character.
Ord converts an ordered type value to its serial number.
Round converts a real value to the integer value after rounding.
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.
Commonly used functions in Delphi 7.0 consist of one or more code sentences to implement a specific function. Using functions can make the code easier to read and understand, speed up programming and reduce repeated code. Similar to a function, a process has no return value, but a function has a return value.
In Delphi 7.0, a lot of functions have been defined for us. There are six types of functions: data Type Conversion Function, String, array operation function, file, disk operation function, memory, pointer operation function, mathematical operation function, date function.
You can call a function in Delphi. Generally, you can use the function directly. However, some functions are not included in the units listed in Uses (the default unit is Windows, Messages, SysUtils, variants, Classes, Graphics, Controls, Forms, Dialogs, so we need to manually add units. For example, the MidStr function is not included in these units, and the unit of the MidStr is included in StrUtils. Therefore, we can add StrUtils to Uses.
In this manual, all functions not included in the default list of units indicate the units to which they belong.
I. Data type conversion functions
In programming, multiple 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 ('20140901', 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. The specific code is: I: = StrToInt ('s \ '+ '64'); then I is equal to 100. Add a 'S \' to convert "hexadecimal" to "decimal ".

4. StrToInt
Function Description: This function is used to convert "integer" to "integer ".
Reference instance: I: = StrToInt ('20140901 ');
Note: it cannot be converted to a type such as StrToInt ('AB') or StrToInt ('hao') because they do not have a number.
  
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.

// Save the New String
Begin
S: = 'I Love China! '; // The following will obtain the "Love" string in I Love China.
MyStr: = Copy (S, 3, 4 );
End;
In the execution result, MyStr is equal to "Love", and the "Love" string is in "I
Begin
S1: = Concat ('A', 'B'); // connects two strings. The S1 variable is equal to AB.
S2: = Concat ('borand', 'delphi ', '123'); // connects three characters. The S2 variable is equal to Borland Delphi 7.0.
End;
Begin
S: = 'I Like Reading CPCW.'; // the following code deletes the "C" character in the S variable.
Delete (S, 16, 1 );
End;
In this case, the S variable is
  
Begin
I: = High (arrText); // The value of I is 9
End;
Begin
S: = 'wat is your name? ';
// Query the word "h" in the above sentence. Use the Insert function to add h.
Insert ('h', S, 2); // Insert "h" from the 2nd position.
End;
6. LeftStr
Begin
S: = 'msn Messenger ';
A: = LeftStr (S, 3); // start from the leftmost and get the three characters on the left. Therefore, A variable is equal to MSN.
End;

// Used to save the string length
Begin
NLen1: = Length ('pcw ');
NLen2: = Length ('computer Report ');
End;
Execution result. nLen1 equals 4 and nLen2 equals 6. Because a Chinese character is equivalent to the length of two characters, the length of three Chinese characters is 6.
  
Begin
I: = High (arrText); // The value of I is 1
End;
Begin
S: = 'abc ';
A: = UpperCase (S); // After the UpperCase function is converted, A is equal to abc.
End;
10. MidStr
  
Begin
S: = MidStr ('China', 1, 2); // The variable Ch
H: = MidStr ('computer report', 1, 1); // The variable H is "Electric ". If the Copy function is used, it should be H: = Copy ('computer Report, 1, 2); otherwise, it will not return the word "electricity. Therefore, it is best to use MidStr when using a string containing Chinese characters.
End;

// The location where the characters used to save the search are located
Begin
NPos: = Pos ('like', 'I Like Reading! ');
End;
In this case, nPos is equal to 3. If not found, nPos is 0.
Note: The Pos function must differentiate the character size during search. If you do not want to differentiate the size, you need to use the UpperCase or LowerCase function to convert the characters (strings) of the two parameters to "UpperCase" or "LowerCase" before searching.
In addition, the AnsiPos function is used to search for characters (strings). The function is used in the same way as the Pos function. When you are searching for Chinese characters, it is best to use the AnsiPos function.
12. RightStr
Begin
S: = 'msn Messenger ';
A: = RightStr (S, 3); // obtain the three characters on the right starting from the rightmost side. Therefore, A variable is equal to ger.
End;

// Define a dynamic array
Begin
SetLength (S, 10); // when set, the S variable can only assign values to strings with a length of 10.
SetLength (arrText, 10); // a Char array with a length of 256 is declared here.
Begin
StrPCopy (arrChar, 'Come on, baby! ');
End;
Begin
S: = 'delphi 7.0 ';
S: = Trim (S );
End;
16. TrimLeft
Function Description: delete spaces on the left of a string (no matter how many spaces on the left are deleted ).
Reference example: S: = TrimLeft ('delphi ');

17. TrimRight
Function Description: delete spaces on the left of a string (no matter how many spaces on the left are deleted ).
Reference example: S: = TrimRight ('delphi ');

Begin
S: = 'abc ';
A: = UpperCase (S); // After the UpperCase function is converted, A is equal to ABC.
End;
  
Begin
S: = 'this is a book .';
AssignFile (F, // open the file in append Mode
Writeln (F, S); // append the content in the S variable to the end of the text.
CloseFile (F); // close the file
End;
Begin
SDir: =
ChDir (sDir );
  
  
Begin
AssignFile (F, // open the file
While not EOF (F) do begin // use the While loop to determine whether the object is not at the end
Readln (F, S); // read a line of text
AllText: = AllText + S;
End;
CloseFile (F); // close the file
End; // used to save the Deletion Status
Begin
IsOK: = // After the function persists, return the result to the IsOK variable. If the IsOK variable is True, the file is successfully deleted.
If IsOK then ShowMessage ('file deleted successfully! ')
Else ShowMessage ('file deletion failed! ');
End;
Begin
IsExists: =
If IsExists then ShowMessage ('windows folder exists! ')
Else ShowMessage ('windows Folder does not exist! ');
End;
Begin
FreeSize: = DiskFree (3); // obtain the remaining space in drive C. The returned value is in bytes.
End;
Begin
DiskSize: = DiskSize (3); // obtain the disk C space. The returned value is in bytes.
End;
  
  
Begin
AssignFile (F, // open the file
While not EOF (F) do begin // use the While loop to determine whether the object is not at the end
Readln (F, S); // read a line of text
AllText: = AllText + S;
End;
End;
Begin
AssignFile (F, // open the file
CloseFile (F); // close the file
Erase (F); // delete an object. You must close the file before deleting it.
End;
Begin
IsExists: =
If IsExists then ShowMessage ('This file exists! ')
Else ShowMessage ('This file does not exist! ');
End;
  
Begin
AssignFile (F, // open the file
NSize: = FileSize (F); // obtain the file size.
Closefile (f); // close the file
End; in this way, the myfolder folder cannot be created successfully. // Open the file
End; // open the file. If the file does not exist, the myfile.txt file will be created on the C drive. If the file exists, all contents in myfile.txt will be overwritten.
End;
  
Begin
Assignfile (F, // open the file
Readln (F, S); // read a line of text to the s Variable
End;
Begin
Assignfile (F, // name it newfile.txt
End;
  
Begin
S: = 'this is a book .';
Assignfile (F, // create a new file
Writeln (F, S); // write the content in the S variable into the text.
End;
Begin
Buffer: = allocmem (256); // The allocated memory size is 256 bytes.
End; // declare an integer pointer
Begin
New (P );
P ^: = 100;
Dispose (p) // release memory
End;
Begin
Getmem (buffer, 256); // allocate a memory size of 256 bytes.
Freemem (buffer); // release memory space
End;
Begin
Getmem (buffer, 256); // allocate a memory size of 256 bytes.
End; // declare an Integer pointer
Begin
New (P); // allocate memory
P ^: = 100; // value assignment
End;
  
Begin
R: = Abs (-2.8); // r equals 2.8
I: = Abs (-156); // I equals 156
End;
2. Exp
Function Description: Exp returns the X power of e, where e is a natural logarithm base.
Reference instance:
E: = Exp (1.0); // e is a real variable.
End;

3. Floor
Function Description: returns the largest integer less than or equal to X.
Reference instance:
Floor (-2.8) =-3
Floor (2.8) = 2
Floor (-1.0) =
Begin
R: = Int (123.456); // R equals 123.0
R: = Int (-123.456); // R equals-123.0
End;

5. Max
Begin
K: = Max (10, 20); // k will be 20
End;

6. Min (unit: math)
 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/sforiz/archive/2010/01/24/5251416.aspx

 

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.