Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I cast a dynamic array of type Char to a string?

Status
Not open for further replies.

Vovin

Programmer
Aug 24, 2003
63
0
0
GB
Hi,

I have a variable:
var searchbuf: array of Char;
This is used to store data from a file and needs to be dynamic.

Later in the code I then try and cast it to a string with the following code:
tempString := String(searchbuf);

The compiler gives me the error '...Incompatible types: 'Array' and 'Char'.

Can anyone tell me how I can get this array of chars into a string. Any help would be much appreciated. Thanks in advance.
 
It's the long way around and I'm sure someone else has a better solution, but you could do something like this:

s := '';
for i := 0 to length(Searchbuf) do
s := s + Searchbuf;

-D
 
Vovin:

I like the following way (assuming short strings would be sufficient)..

Var
SearchBuf : Array[0..256] of Char;
TempString : ShortString ABSOLUTE SearchBuf;
Begin
FillChar(SearchBuf, SizeOf(SearchBuf), 0);
SearchBuf[1] := 'A';
SearchBuf[2] := 'B';
:
:
SearchBuf[0] := Chr(strlen(@SearchBuf[1]));
ShowMessage(TempString);
End;

HTH,
JGS
 
RichardCHaven:

Very elegant indeed.

However, I did notice that due to the fact I still work with Short Strings as a default, I needed to change your string(pchar( to AnsiString(pchar( to get large strings to display over 255 chars.

Thanks for the tip.

JGS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top