c# - Read line from byte array (not convert byte array to string) -


I have a byte array that I'm reading from NetworkStream. The first two bytes tell the length of the packet and then the packet is read in a byte array of that length. There are some strings in the data I need to read from the networkstream / byte array, i.e. variable length data that is ending with new line characters, and certain width fields such as bytes and long So, something like this:

  // I would have demarcated them for clarity, but I did not mean that // means that the stream has been delimited because it is not. StringbyteStringStringbytebytebytelonglongbytelonglong  

I have read (and say something) that is coming in the format of data packets, and what I have to do is read a "row" for each string value, but bytes And read a certain number of bytes for the lords. So far, my proposed solution is to use a while loop to read the bytes in a temporary byte array, unless there is a new letter. Then, convert the byte to string. It seems to me a kidney, but I do not see another clear path. I know that I can use the StreamReader.ReadLine () , but it will include another stream and I already have a NetworkStream but if it is better The solution is, so I'll give it a shot.

The other option I have considered, my backend team has to write a byte or two for the length of the string value so that I can read length and then the string based on the specified length.

So, as you can see, how do I have to go about this, there are some options for this, and I want to see your input on what you would consider to be the most The best way here is the code that I just have to read in the whole packet as a string. The next step is to break the different areas of the packet and do the actual programming work, which is necessary, creating objects, updating the UI, etc. Depending on the data in the packet

  string line = zero; While (stream.DataAvailable) {// Get the packet length; UInt16 packet leng = 0; Header = new byte [2]; Stream Read (Header, 0, 2); // If the architecture is very low, then the header array for the Bitikonver class has to be reversed (Bitakonarver.IsLLTEEndian) array Aver (header); PacketLength = BitConverter.ToUInt16 (header, 0); Buffer = new byte [packet lang]; Stream Reed (buffer, 0, bitonarar.touuent16 (header, 0)); Line = system Text. ASCIIEncoding.ASCII.GetString (buffer); Console.WriteLine (line); }  

Personally I

  1. An Int16 Keep in the beginning of the stars, you know how long they will last, and
  2. use the IO.BinaryReader class to read, in the variables such as "read", ints, strings, letters etc. BinReader.ReadInt16 () will read two bytes, int16 they represent, and move two bytes in the stream

Hope it helps.

Be cautious of using the redstrace method, it assumes that the string is predefined with a custom 7 bit integer, that is, it was written by the Binaryworld class. The following is from this post.

There are two ways to write a two-dimensional string: the overloaded type () method and method of writing () method. By typing the pre string as a stream of bytes in accordance with the encoding, which is using the class. WriteString () method also uses specified encoding, but it pricks the string byte with the actual length of the string. Such prefix strings are read back via BinaryReader.ReadString ().

The interesting thing about length holds that as much as possible bytes are used to capture this size, it is stored as a type called a 7-bit encoded integer If a single byte is used in length 7 bits, if it is more than this then the high bit is set on the first byte and the value of the 7 beats is moved and the second byte is made. It is repeated with repeated bytes until there are not enough bytes to hold the value. This mechanism is used to ensure that the length is not an important part of the shape taken by the serialized string. BinaryWriter and BinaryReader have 7-bit encoded integer methods for reading and writing, but they are protected and you can use it only when you get them from these sections.


Comments