?
Solved

parsing a byte-buffer

Posted on 2000-04-03
11
Medium Priority
?
296 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
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.

 
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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
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…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

770 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