• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

parsing a byte-buffer

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
d32coder
Asked:
d32coder
  • 4
  • 2
  • 2
  • +2
1 Solution
 
edeyCommented:
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
 
edeyCommented:
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
 
TussinCommented:
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
Independent Software Vendors: 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!

 
MadshiCommented:
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
 
mullet_attackCommented:
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
 
MadshiCommented:
That's right.
0
 
MadshiCommented:
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
 
mullet_attackCommented:
I stand corrected. Pos skips over #0 as if it was any other character....
0
 
MadshiCommented:
Well, you were right about StrPos (my original suggestion).   :-)
0
 
d32coderAuthor Commented:
This is exactly what I needed.  Thank you.
0
 
d32coderAuthor Commented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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