Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String Question...

Posted on 2003-11-18
15
Medium Priority
?
910 Views
Last Modified: 2008-02-01
How do i convert a PAnsiChar string that is retrieved from a record back to a normal string:

ie: if the PAnsiChar string is returned as:  aaaa#0bbbb#0cccc#0dddd#0

i want to break the PAnsiChar string up into a normal string that will have aaaabbbbccccdddd in it
0
Comment
Question by:mantra246
[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
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 3

Expert Comment

by:Mamouri
ID: 9775987
Hi!

Just use StrCopy fiunction. It must solve the problem

Regards
0
 

Author Comment

by:mantra246
ID: 9776103
StrCopy and StrLCopy only copies to the first #0  - i need to be able to get the entire line.
0
 
LVL 6

Expert Comment

by:zebada
ID: 9776289
PAnsiChar strings are defined to end at the first #0.
How do you decide where the end of your data is?

Regards
Paul
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:mantra246
ID: 9776301
It is information that is returned by windows in a record - the record contains a list of items with each being ended by a #0 - when i try to get the list it only gives me the first one and not the rest that i know is there.
0
 
LVL 6

Expert Comment

by:zebada
ID: 9776866
Then you should define a record in Delphi to accept the data.
For your example data:

TMyRecord = record
  astr: array[0..4] of char; /* Note: must start with 0 index and be large enough to accommodate the #0 terminator char. */
  bstr: array[0..4] of char;
  cstr: array[0..4] of char;
  dstr: array[0..4] of char;
end;

var
  myrecord: TMyRecord;
  var a,b,c,d: string;
begin
  SomeWindowFunction(@MyRecord);
  a := PChar(MyRecord.aStr);
  b := PChar(MyRecord.bStr);
  c := PChar(MyRecord.cStr);
  d := PChar(MyRecord.dStr);
end;

By the way most windows structures are already defined as Delphi records.

Regards
Paul
0
 
LVL 7

Expert Comment

by:jconde
ID: 9776993
what about

procedure TForm1.Button1Click(Sender: TObject);
const
  ANSI_LENGTH = 20;
var
  P: PAnsiChar;
  S: String;
  I: Integer;
begin
  P := 'aaaa' + #0 + 'bbbb' + #0 + 'cccc' + #0 + 'dddd' + #0;
  for I := 0 to ANSI_LENGTH do
  begin
    If (P + I)^ <> #0 then
      S := S + (P + I)^;
  end;
  showmessage(s);
end;
0
 
LVL 7

Expert Comment

by:jconde
ID: 9777025
my above code has a minor bug ... it should be:

for I := 0 to ANSI_LENGTH -1 do

instead of

for I := 0 to ANSI_LENGTH  do
0
 
LVL 7

Expert Comment

by:jconde
ID: 9777036
Since you're dealing with binary data, you MUST know the length of the PAnsiChar variable ... That's unless you have a terminating character at the end of the PAnsiChar string
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9777686
I guess the end is marked by a double #0, so walk through the data until you're at the end of it all. An example:

program Project1;

{$APPTYPE CONSOLE}

const
  Data = 'aaaa'#0'bbbbb'#0'ccccccccccc'#0#0;

var
  Value: PAnsiChar = Data;
  Loop: PAnsiChar;
  Line: string;

begin
  Line := Value;
  Loop := Value;
  while (Length(Line) > 0) do begin
    WriteLn(Line);
    while (Loop^ <> #0) do
      Inc(Loop);
    Inc(Loop);
    Line := Loop;
  end;
  Write('Press <Enter>. ');
  ReadLn;
end.

However, if the end of the list isn't ended with a double #0#0 but if you have a fixed amount of elements then you'll need to loop <number_of_items> times through the string value.
0
 
LVL 9

Expert Comment

by:mocarts
ID: 9778082
hi, all :)
just my two cents..
basically code is same as provided by Alex, but shorter (not faster)

procedure SplitPCharArray(aList: TStrings; pac: PAnsiChar);
var
  l: integer;
begin
  aList.BeginUpdate;
  try
    aList.Clear;
    l := StrLen(pac);
    while l > 0 do begin
      aList.Add(pac);
      inc(pac, l+1);
      l := strLen(pac);
    end;
  finally
    aList.EndUpdate;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SplitPCharArray(Memo1.Lines, 'aaaa'+#0+'bbbb'+#0+'cccc'+#0#0);
end;

wbr, mo.
0
 
LVL 9

Accepted Solution

by:
mocarts earned 800 total points
ID: 9778172
oops :) you don't  need to split :) ok, here will be join.

function JoinPCharArray(pac: PAnsiChar): string;
var
  l: integer;
begin
  l := StrLen(pac);
  while l > 0 do begin
    Result := Result + pac;
    inc(pac, l+1);
    l := strLen(pac);
  end;
end;
0
 
LVL 7

Expert Comment

by:jconde
ID: 9780733
I think mocarts has it ....
0
 
LVL 6

Expert Comment

by:zebada
ID: 9781744
> It is information that is returned by windows in a record - the record contains a list of items with each being ended by a #0

What is Windows definition of the record?
Why would you want to "parse" the record structure yourself?
That seems like a very backwards way of getting the returned data from the Window's function.
I cannot possibly imagine that Windows would pass back a string constructed like that.

Regards
Paul

0
 
LVL 7

Expert Comment

by:jconde
ID: 9781836
zebada,

Although I agree its a very weird way of inter-communicating with a "windows record", I do know several News Feed agents transmit their information (via a VPN, Cable, E0, etc..) just like that ...
0
 
LVL 6

Expert Comment

by:zebada
ID: 9781893
Good point!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

660 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