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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:34:53+00:00 2026-05-11T08:34:53+00:00

I have a byte array that I am reading in from a NetworkStream. The

  • 0

I have a byte array that I am reading in from a NetworkStream. The first two bytes tell the length of the packet that follows and then the packet is read into a byte array of that length. The data in that I need to read from the NetworkStream/byte array has a few Strings, i.e. variable length data terminated by new line characters, and some fixed width fields like bytes and longs. So, something like this:

// I would have delimited these for clarity but I didn't want // to imply that the stream was delimited because it's not. StringbyteStringStringbytebytebytelonglongbytelonglong 

I know (and have some say in) the format of the data packet that is coming across, and what I need to do is read a ‘line’ for each string value, but read a fixed number of bytes for the bytes and longs. So far, my proposed solution is to use a while loop to read bytes into a temp byte array until there is a newline character. Then, convert the bytes to a string. This seems kludgy to me, but I don’t see another obvious way. I realize I could use StreamReader.ReadLine() but that would involve another stream and I already have a NetworkStream. But if that’s the better solution, I’ll give it a shot.

The other option I have considered is to have my backend team write a byte or two for those String values’ lengths so I can read the length and then read the String based on the length specified.

So, as you can see, I have some options for how to go about this, and I’d like your input about what you would consider the best way to do it. Here’s the code that I have right now for reading in the entire packet as a string. The next step is to break out the various fields of the packet and do the actual programming work that needs to be done, creating objects, updating UI, etc. based on the data in the packet.

string line = null;   while (stream.DataAvailable) {       //Get the packet length;       UInt16 packetLength = 0;       header = new byte[2];       stream.Read(header, 0, 2);       // Need to reverse the header array for BitConverter class if architecture is little endian.       if (BitConverter.IsLittleEndian)         Array.Reverse(header);       packetLength = BitConverter.ToUInt16(header,0);      buffer = new byte[packetLength];     stream.Read(buffer, 0, BitConverter.ToUInt16(header, 0));     line = System.Text.ASCIIEncoding.ASCII.GetString(buffer);     Console.WriteLine(line); } 
  • 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-11T08:34:54+00:00Added an answer on May 11, 2026 at 8:34 am

    Personally I would

    1. Put an Int16 at the start of the strings, so you know how long they’re going to be, and
    2. Use the IO.BinaryReader class to do the reading, it’ll ‘read’, ints, strings, chars etc into variable e.g. BinReader.ReadInt16() will read two bytes, return the int16 they represent, and move two bytes on in the stream

    Hope this helps.

    P.S. Be careful using the ReadString method, it assumes the string is prepended with custom 7 bit integers i.e. that it was written by the BinaryWriter class. The following is from this CodeGuru post

    The BinaryWriter class has two methods for writing strings: the overloaded Write() method and the WriteString() method. The former writes the string as a stream of bytes according to the encoding the class is using. The WriteString() method also uses the specified encoding, but it prefixes the string’s stream of bytes with the actual length of the string. Such prefixed strings are read back in via BinaryReader.ReadString().

    The interesting thing about the length value it that as few bytes as possible are used to hold this size, it is stored as a type called a 7-bit encoded integer. If the length fits in 7 bits a single byte is used, if it is greater than this then the high bit on the first byte is set and a second byte is created by shifting the value by 7 bits. This is repeated with successive bytes until there are enough bytes to hold the value. This mechanism is used to make sure that the length does not become a significant portion of the size taken up by the serialized string. BinaryWriter and BinaryReader have methods to read and write 7-bit encoded integers, but they are protected and so you can use them only if you derive from these classes.

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

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • 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
  • added an answer No, you shouldn't use your own password-hashing for MySQL authentication.… May 11, 2026 at 10:42 am
  • added an answer The answer is what requirements will the machine need in… May 11, 2026 at 10:42 am
  • added an answer For the graphical output you can use the Linux framebuffer,… May 11, 2026 at 10:42 am

Related Questions

I have a byte array that represents a complete TCP/IP packet. For clarification, the
Pretty simple scenario. I have a web service that receives a byte array that
I have a byte array in memory, read from a file. I would like
I have a byte[] array, the contents of which represent a TIFF file (as
I have an EXE loaded into a byte array, and I am trying to
I have a byte stream I need parsed into a struct, and I also
I have a twenty byte hex hash that I would like to store in
I have a variable that contains a 4 byte, network-order IPv4 address (this was
I have a string containing byte data. How can I perform an in-place conversion
I have a unit test where I have statically defined a quite large byte

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.