Solved

# Converting an array of char to a string...

Posted on 1998-03-21
454 Views
Hi...
Very simple. How do I convert an array of char to a string variable? I have a variable x which is declared as array[1..200] of char and I have a string variable y. I simply want the contents of x to go into y. I tried the 'Strpas' function to convert a null-terminated string to Pascal type sting but it gave me an error 'Incompatible types : Array and PChar'. There must be an easy way to do this. Can you help?

Cheers!
Shawn Halfpenny
drumme59@sprint.ca
0
Question by:aztec
• 2
• 2
• 2
• +1

LVL 5

Expert Comment

ID: 1360687
procedure TForm1.Button1Click(Sender: TObject);
var A: array[1..200] of char;
begin
FillChar(A, SizeOf(A), 'S');
showmessage(StrPas(PChar(@A)));
end;
0

LVL 5

Expert Comment

ID: 1360688
Actually you can write:
showmessage(StrPas(@A));
0

LVL 4

Expert Comment

ID: 1360689
If you use D2 or D3, you could simply typecast with
y := String(x);

Slash/d003303
0

Author Comment

ID: 1360690
Hello...

Regards,
Shawn Halfpenny
0

LVL 4

Expert Comment

ID: 1360691
You have to reject the answer and delete the question yourself 'cause you are the owner of it. But the main thing is that it is solved.

Slash/d003303
0

Author Comment

ID: 1360692
OK, I rejected the answer, now how do I delete this question? There is no selection to delete it!

Shawn Halfpenny
0

LVL 6

Accepted Solution

Stuart_Johnson earned 50 total points
ID: 1360693
This is easy.  Firstly, you cant convert an Array of Chars to a String which doesnt have a Zero Element.  Ie, you can convert an Array[0..199] of Char to a String (using StrPas), but you cant convert an Array[1..200] of Char to a String.

So, simply redeclare X as Array[0..199] of Char, use the StrPas function, Y := StrPas(X) and Bobs' your Uncle!

Unfortunately, there is no other way.  A PChar is a null terminated string - the first character depicts the length of the string.  Therefore, you need to start your character array with a zero element.

Hope this helps,

Stu
0

## Featured Post

### Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…