Delphi Char Array, string, and Pchar conversions

Source: Internet
Author: User

Because to call the Windows API or to the VC + + Write interface, a lot of places to use Pchar, now the char array, string and pchar between the conversion are listed, are online looking for information, I summarize, first directly on the code, and then the principle.

1.string conversion to Pchar

You can use Pchar to force type conversions, or you can use the Strpcopy function

var   S:string;  P,p1:pchar; begin   s:='Hello Delphi';  P:=PChar (s);  ShowMessage (p);  P1:=stralloc (Length (s) +1);  Strpcopy (p1,s);  ShowMessage (p1);  Strdispose (p1); End;

2.pchar Conversion to String

Pchar can be used directly as a string, or you can use the Strpas function to convert

var   s,s1,s2:string;  P:pchar; begin   s:='Hello Delphi';  P:=PChar (s);  ShowMessage (p);  S1:=p;  ShowMessage (S1);  S2:=Strpas (p);  ShowMessage (S2); End;

3.char Array converted to string
Get the first address of an array using the Strpas function

varC:Array[0.. One] ofChar; S:string;beginc[0]:='H'; c[1]:='e'; c[2]:='L'; c[3]:='L'; c[4]:='o'; c[5]:=' '; c[6]:='D'; c[7]:='e'; c[8]:='L'; c[9]:='P'; c[Ten]:='h'; c[ One]:='I'; S:=strpas (@c[0]); ShowMessage (s);End;

4.string Turn char Array

Use the move or CopyMemory function

varS:string; C:Array  ofChar; I:integer;beginS:='Hello Delphi';  SetLength (C,length (s)); //Move (S[1],c[0],length (s));//move and CopyMemory.CopyMemory (@c[0],pchar (s), Length (s));  forI:=low (c) toHigh (c) Do  beginShowMessage (string(C[i]))End;End;

5.char Array Turn Pchar

var   C:array [0...  One  of Char;  P:pchar; begin   c:='Hello Delphi';   // P:[email protected][0];  P:=pchar (@c[0]);  ShowMessage (Strpas (P)); End;

6.pchar Turn char Array

Use the move or CopyMemory function

varS:string;  P:pchar; C:Array  ofChar; I:integer;beginS:='Hello Delphi'; P:=PChar (s);  SetLength (C,length (s)); //Move (P^,c[0],length (s));//move and CopyMemory.CopyMemory (@c[0],p,length (s));  forI:=low (c) toHigh (c) Do  beginShowMessage (string(C[i]))End;End;

Now talk about char arrays, pchar, and string

Both string and char arrays are a piece of memory that holds contiguous characters. String the memory of the specific character is transparent to the user, managed by Delphi to allocate, copy and release, the user cannot intervene (in fact, it can, but through the illegal way). The char array doesn't have to be said, right? Pchar is a pointer, it is only 32 bits in size. When defined, it is automatically filled by Delphi 0. To use Pchar as a string, you must allocate the memory yourself and you must release it yourself. The Pchar string consists of #0, which represents the end of the string, and the operation of the related Pchar string provided by Delphi is determined by the #0 to determine the end of the string.
Because Pchar is a pointer, it can point anywhere (that is, it does not have to point to a string). Assigning a string to Pchar is simply the address of the memory that holds the specific string in string to the Pchar variable. Of course, you can also give the address of the first element of the char array to Pchar.


As to which memory is small, char array If the empty string is so Pchar There is no doubt that the string is the slowest from speed, for example:
When passed as a parameter (not a var call) to the procedure when string passes a copy of the entire string past, Pchar passes the copy of the pointer itself past (32-bit), and the char array, like Pchar, passes the address copy of the first element. But for flexibility, the string is the highest, And Delphi supports the most functions. You can also use a string as a buffer (because it can contain character 0).

Note: Because the string and char arrays are contiguous, the pointer to the first address of string is @s[1], and the pointer to the first address of the char array is @c[0]. Pchar is the end of #0, so a lot about Pchar

Use of the function, such as when using the Stralloc function to allocate memory to Pchar and when using the Strpcopy function

7. Use the Stralloc function to convert a string to Pchar

var   P:pchar;  S:string; begin   s:='ABCDEF';  P:=Stralloc (Length (s));  Move (s[1],p^,length (s));  ShowMessage (Strpas (P));  Strdispose (p); End;

Execute the above code, the string is garbled after, view the Delphi help inside about Stralloc function

Delphi's help is as follows:

Stralloc allocates a buffer for a null-terminated string with a maximum length ofSize-1 (1 byte must is reserved for the termination character).

The Stralloc function assigns the strength memory of the string to Pchar to size-1 bytes, so we'll allocate a byte of memory to save the Pchar end flag #0

The correct use is as follows:

var   P:pchar;  S:string; begin   s:='ABCDEF';  P:=stralloc (Length (s) +1);  Move (s[1],p^,length (s) +1); // Copy the contents of S into P  in bytes // Move (S[1],p^,length (s));//execute this sentence will appear garbled   showmessage (Strpas (P));  Strdispose (p); End;

8. Convert a string to a char array using strpcopy

varS:string; C:Array  ofChar; I:integer;beginS:='Hello Delphi';  SetLength (C,length (s)); Strpcopy (@c[0],s);  forI:=low (c) toHigh (c) Do  beginShowMessage (string(C[i]))End;End;

The above code execution time will be error, check Delphi help only know, strpcopy function does not do length check, need programmer own control.

Delphi helps as follows:

Strpcopy copies Source into a null-terminated string Dest. It returns a pointer to Dest.
Strpcopy does not perform any length checking.
The destination buffer must has a guest-at least Length (Source) +1 characters

The correct code is as follows:

varS:string; C:Array  ofChar; I:integer;beginS:='Hello Delphi'; SetLength (C,length (s)+1); Strpcopy (@c[0],s);  forI:=low (c) toHigh (c) Do  beginShowMessage (string(C[i]))End;End;

When using the Strpcopy function, make sure that the length of the destination Pchar is at least the length of the source string +1

Delphi Char Array, string, and Pchar conversions

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.