Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding string data in byte buffer array

Status
Not open for further replies.

rsballard

Technical User
Oct 1, 2002
16
US
I have a binary file that is a a bunch of consequtive
type x = record
header: array[1..256] of byte;
data: array[1..10000] of single;
end

The spec calls for a text string of data to appear at the same location of the header in each of these records. We'll it doesn't. It may move around a little or even sometimes the first of the string is missed and only partial data is written to the header.

I need to search for the head of the wanted string, allowing it to move 10-20 bytes or so and retrieve the index into the array for the head of the string. If it's not found within some reasonable range, return -1.

I can't make anything work. I need something like the PosEx function that will find a substring in a byte array.

Any help?

Thanks
Bob
 
SetLength(TestString, DesiredScanLength);
Move(InstanceOfX, TestString[1], DesiredScanLength);
Hit := Pos(SearchKey, TestString);
 
The function that you require is StrPos which will locate a substring in a byte array. The function will return a pointer to the substring if it finds it otherwise it returns nil.

You will need to do convert the returned pointer to a numeric value.

For example, using your definition of the X record.
Code:
function FindSubStringInX ( RecX: X; target: string ): integer;
var
  ptr: pchar;
begin
  ptr := StrPos ( @RecX.header, target );
  if ptr = nil then
    result := -1
  else
    result :=  ptr - @RecX.header + 1;  // +1 because array is [1..256]
end;
 
Thanks, gents. Sometimes I just need to discuss things to make the mental connection with my problem.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top