Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:11:32+00:00 2026-05-28T00:11:32+00:00

how to search a hex in stream Efficiently and quickly? if the stream is

  • 0

how to search a hex in stream Efficiently and quickly?
if the stream is big ,how to search quickly in the stream and retun the postion ?

function FindInMemStream(Stream: TMemoryStream; What: String):Integer;
var
  bufBuffer, bufBuffer2: array[0..254] of Char;
  i: Integer;
begin
  Result := 0;
  i := 0;
  FillChar(bufBuffer, 255, #0);
  FillChar(bufBuffer2, 255, #0);
  StrPCopy(@bufBuffer2, What);
  Stream.Position:=0;
  while Stream.Position <> Stream.Size do
  begin
    Stream.Read(bufBuffer[0],Length(What));
    if CompareMem(@bufBuffer,@bufBuffer2,Length(What)) then
    begin
      Result := Stream.Position-Length(What);
      Exit;
    end;
    i := i + 1;
    Stream.Seek(i,0)
  end;
end; 

i want to change the function to serch hex ,is this function Efficient? can u give me a Efficient function to serch hex ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:11:33+00:00Added an answer on May 28, 2026 at 12:11 am

    I have adapted the code from POS function available in system.pas 🙂
    If you need to find some hex, you can use like:

    PosHex(#$15#$AA#$04, MyStream);
    
    function PosHex(const SubStr: AnsiString; const StrStream: TMemoryStream): Integer;
    var
      SubLen, SrcLen, Len, I, J: Integer;
    
      C1: AnsiChar;
      Str: PAnsiChar;
    begin
      SrcLen := StrStream.Size;
      SubLen := Length(SubStr);
    
      Result := 0;
      if (SubLen <= 0) or (SrcLen <= 0) or (SrcLen < SubLen) then Exit;
    
      StrStream.Position := 0;
      Str := StrStream.Memory;
    
      Len := SrcLen - SubLen + 1;
      C1 := SubStr[1];
      for I := 1 to Len do
      begin
        if Str[I] = C1 then
        begin
          Result := I;
          for J := 1 to SubLen-1 do
          begin
            if Str[I+J] <> SubStr[1+J] then
            begin
              Result := 0;
              break;
            end;
          end;
          if Result <> 0 then Exit;
        end;
      end;
    end;
    
    
    Usage:
    procedure TForm3.FormCreate(Sender: TObject);
    Var
      M: TMemoryStream;
      S: AnsiString;
    begin
     S := 'I would like to Find This string!';
    
     M := TMemoryStream.Create;
     M.WriteBuffer(S[1], Length(S));
    
     Memo1.Lines.Add(IntToStr(PosHex('Find This', M)));
    
     M.Free;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I search a function in MySQL to do conversion between hex string to
I ran the following command at linux bash: apt-cache search 'hex.*(view|edit)' My intention was
Anyone know the syntax to search a MySQL table column by hex value or
/* * PURPOSE * Search if a string contains a string and print it
In perl I am trying to use a regex to search through specific hex
Search for: 'chemist' Problem: query which will match a string like 'onechemist' but not
Search the web for the phrase I cosay. I run across this phrase being
Search Engine Optimization (SEO) seems to be a topic I'm running into more and
Search for good JAVA lib for playing with POST-GET requests - Is there any
Search for good Java lib for playing with Sockets connections - Is there any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.