Solved

parsing a byte-buffer

Posted on 2000-04-03
11
295 Views
Last Modified: 2010-04-04
I loaded a file into a TFileStream with TFilestream.Readbuffer()  The data resides in a Buffer: Array of Btye;

How can I parse this buffer to find the offset of a known string?  

0
Comment
Question by:d32coder
[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
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2682303
a string is just a bunch of chars, which are really bytes, compare the ord value ( ord(my_string[0]) ) to a byte in the buffer to find the first charecter, check if the next byte is the second char, and so on....


GL
Mike
0
 
LVL 6

Expert Comment

by:edey
ID: 2682305
Should think you'd may also be able to recast the array of byte as PChar, and get your string conversion for free, then all you have to do is use the pos function to find your string


GL
Mike
0
 
LVL 2

Expert Comment

by:Tussin
ID: 2682383
if that file is not too bigl, I think use TMemoryStream is better. TMemoryStream provides propery "Memory" which you are able to access its content directly. You also can use TMemoryStream.LoadFromFile(FileName) to load file's content to memory.

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!

 
LVL 20

Expert Comment

by:Madshi
ID: 2682704
The function StrPos(@Buffer, 'Known string') gives you the pointer to the byte in "Buffer", where the known string was found the first time.
To calculate the offset into the Buffer do this:
 
var offset : integer;
begin
  pchar(offset) := StrPos(@Buffer, 'Known string');
  if offset <> 0 then
    pchar(offset) := pchar(offset) - pchar(@Buffer);

Regards, Madshi.
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2683322
I would probably use Edey's first suggestion "find the first charecter, check if the next byte is the second char, and so on.... ". The pchar approaches would most likely have problems if there were #0's in the file, eg binary file.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2683347
That's right.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 2683354
But you could do this:

var s1 : string;
begin
  with TFileStream.Create('c:\test.dat', fmOpenExisting) do
    try
      SetLength(s1, Size);
      ReadBuffer(pointer(s1)^, Size);
      // Here you can use "Pos"
    finally Free end;

The Delphi "string" type doesn't look for #0 characters. So this should work perfectly.

Regards, Madshi.
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2683559
I stand corrected. Pos skips over #0 as if it was any other character....
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2683601
Well, you were right about StrPos (my original suggestion).   :-)
0
 

Author Comment

by:d32coder
ID: 2685327
This is exactly what I needed.  Thank you.
0
 

Author Comment

by:d32coder
ID: 2685332
Your responses were fantastic.  I am very impressed with this site and all of you.  Thanks a million.  I hope I can return the favor as I get better with Delphi.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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