BinSearch Function

Action

Searches binary data for either a string or a specified amount of binary data.

Include file

Kernel.bdh

Syntax

BinSearch( in sSource     : string,
           in nSourceSize : number,
           in sSearch     : string,
           in nSearchSize : number optional,
           in nOptions    : number optional,
           in nAlign      : number optional ): number;

Return value

Position index where the data to search for was found, or zero if the substring was not found or an error occurred. Note that the index of the first byte is one (1), not zero (0).

Parameter Description
sSource Binary data to search
nSourceSize Size of binary data to search
sSearch String or binary data to look for
nSearchSize Size of the binary data to look for (optional). When searching binary data for a string, this parameter can be omitted
nOptions

Specifies how to search the specified binary data (optional). By passing one of the following parameters to the function, it is possible to determine whether to start a new search or to continue searching

  • STR_SEARCH_FIRST. Search for the first occurrence (default)

  • STR_SEARCH_NEXT. Continue a search started before

In addition, you can determine to search binary data for a string case-sensitively by passing the STR_SEARCH_MATCH_CASE flag to the function:

Passing the STR_SEARCH_REVERSE flag to the function specifies to search the binary data in reverse order.

nAlign Search alignment (default). If an alignment is specified, the function checks both if the data is found, and, if so, whether it is aligned to a multiple of the specified value

Example

dcltrans
  transaction TBinSearch
  var
    nPos, nDir: number;
  begin
    nPos := BinSearch("DECDEFG", 0, "DE");
    write(nPos); writeln;

    nPos := BinSearch("\h00010203040506", 7,"\h030405", 3);
    write(nPos); writeln;

    nDir := STR_SEARCH_FIRST;
    loop
      nPos := BinSearch("\h01000001000001", 7, "\h01", 1, nDir);
      if nPos = 0 then exit end;
      write(nPos); writeln;
      nDir := STR_SEARCH_NEXT;
    end;
  end TBinSearch;