Solved

Converting an array of char to a string...

Posted on 1998-03-21
7
465 Views
Last Modified: 2010-04-06
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
Comment
Question by:aztec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:ronit051397
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

by:ronit051397
ID: 1360688
Actually you can write:
showmessage(StrPas(@A));
0
 
LVL 4

Expert Comment

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

Slash/d003303
0
Industry Leaders: 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!

 

Author Comment

by:aztec
ID: 1360690
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
 
LVL 4

Expert Comment

by:d003303
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

by:aztec
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

by:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question