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 51965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:49:58+00:00 2026-05-10T16:49:58+00:00

Searching for a string within a string is extremely well supported in .NET but

  • 0

Searching for a string within a string is extremely well supported in .NET but what do you do when the data you need to search isn’t a string?

I have binary data arriving in regular chunks via a NetworkStream. Packets are binary but they all start with a signature sequence of bytes. I accumulate the chunks into a larger buffer and look for the start-of-packet signature.

What I’m really looking for is the byte[] equivalent to the String.IndexOf(ss) method. I have a nasty feeling I’m going to have to implement this myself with a loop and a state machine.

Any suggestions? Over to you!


As suggested, Array.IndexOf(byte) will at least save me an explicit loop. Since posting, it occurred to me to find the first signature byte, then probe ahead for a match where the last signature byte should be, then if they both match try a brute-force comparison for the rest of the string. This approach has the advantage of rejecting false matches cheaply and allowing me to cheaply reject when I have a partial signature pending another chunk.

Google reveals that the above brilliant plan is a degenerate case of ‘KMP’ or Knuth-Morris-Pratt algorithm. On the bright side if Knuth put his name on it it’s probably greased lightning, on the downside why is it that whenever I have a good idea Donald Knuth thought of it 25 years ago?

Since I can’t award the points to Donald Knuth I guess they go to Nelson.

  • 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. 2026-05-10T16:49:59+00:00Added an answer on May 10, 2026 at 4:49 pm

    You can use Array.IndexOf to find a single byte.

    However, I would caution you that some valid data might accidentally be your signature and completely throw off your application. A better solution in my opinion would be to always send a four byte integer that contains the size of the packet. Then read that many bytes in to clear the buffer of that packet.

    If you are using TCP it is completely acceptable to kick a client off if they lie about packet size or request a stupid amount of memory 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer So I took a slightly different approach. When I create… May 12, 2026 at 12:06 am
  • Editorial Team
    Editorial Team added an answer On Windows Systems you can get the environment variable PROCESSOR_ARCHITECTURE.… May 12, 2026 at 12:06 am
  • Editorial Team
    Editorial Team added an answer Unit testing is only a (very valid) alternative to test… May 12, 2026 at 12:06 am

Related Questions

I feel like I must be missing something completely obvious, but I don't see
I have a daemon that reads a configuration file in order to know where
Maybe I'm searching with the wrong keywords, but I can't find a solution to
I have an ASP.NET page that uses a repeater nested within another repeater to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.