[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

Converting an array of char to a string...

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
aztec
Asked:
aztec
  • 2
  • 2
  • 2
  • +1
1 Solution
 
ronit051397Commented:
procedure TForm1.Button1Click(Sender: TObject);
var A: array[1..200] of char;
begin
 FillChar(A, SizeOf(A), 'S');
 showmessage(StrPas(PChar(@A)));
end;
0
 
ronit051397Commented:
Actually you can write:
showmessage(StrPas(@A));
0
 
d003303Commented:
If you use D2 or D3, you could simply typecast with
y := String(x);

Slash/d003303
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aztecAuthor Commented:
Hello...
  I inadvertantly submitted this same question twice (..see my question history). It has already been answered. Please delet this one.

Regards,
   Shawn Halfpenny
0
 
d003303Commented:
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
 
aztecAuthor Commented:
OK, I rejected the answer, now how do I delete this question? There is no selection to delete it!

Shawn Halfpenny
0
 
Stuart_JohnsonCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now