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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:56:06+00:00 2026-05-11T05:56:06+00:00

actually i’m working with .Net Framework 3.5, so i have all these nice little

  • 0

actually i’m working with .Net Framework 3.5, so i have all these nice little features like lambdas, linq, etc.

Given is a serial connection (or to be more abstract: a stream) where you receive some data, which will be in a format like this:

struct Packet {     byte STX     UInt16 DataLength     string Data     byte CRC     byte ETX } 

Using a simple mapping of the incoming data doesn’t help due to the fact, that you don’t really know how long one packet will be, cause it’s written within the structure (the DataLength).

So my first idea would be to read the stream by byte and put it into ???. Yes, that’s the next question. Where to store this first Raw data? Into a simple byte array, with the maximum possible length (that would be 65540 bytes, due to the fact, that DataLength is an UInt16 plus the additional bytes from the other fields). Or should i open up a Queue and fill it up will all the incoming bytes or maybe exists there another nice possibility?

Let’s assume these problems are cleared and i have some kind of local buffer, that holds all the raw bytes from the stream. What’s the nicest way to interpret it by the given structure?? Just doing some kind of for- or foreach-loop or exists there a smarter (with better performance) way (e.g. with regex or linq)?

Best regards, Oliver

  • 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-11T05:56:06+00:00Added an answer on May 11, 2026 at 5:56 am

    How about…

    struct Packet {     public byte STX;     public UInt16 DataLength;     public string Data;     public byte CRC;     public byte ETX; }  //Warning: Need to add error handling class PacketReader {     private BinaryReader _reader;      public PacketReader(Stream stream)     {         _reader = new BinaryReader(stream);     }      Packet ReadPacket()     {         var packet = new Packet()              {                 STX = _reader.ReadByte(),                 DataLength = _reader.ReadUInt16(),                 Data = Encoding.ASCII.GetString(                     _reader.ReadBytes(packet.DataLength)),                 CRC = _reader.ReadByte(),                 ETX = _reader.ReadByte()             };          return packet;     } } 

    Please note: I have not used BinaryReader.ReadString() on purpose because it is designed to operate on strings generated by BinaryWriter.WriteString(). The encoding is a bit different even though its a length prefixed string.

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

Sidebar

Related Questions

Actually my question is all in the title. Anyway: I have a class and
Actually, this question seems to have two parts: How to implement pattern matching? How
Actually, I'm using this way. Do you have a better way? private bool AcceptJson(HttpRequest
Actually here is a similar topic with little practical value. As far as I
Actually what i am trying to build is like a kind of firewall. It
I actually have an answer to my question but it is not parallelized so
I actually have two questions regarding the same problem but I think it is
Actually there many examples and I have used one of them. But it works
actually i have 2 tables table1 and table2 table1 name city addr. table2 name
Actually I am interested in Java AA framework that supports many types of A&A.

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.