2

I am reading a message from my network connection which is serialized as a series of null-terminated strings (and other binary data)

I could read a char at a time using a BinaryReader, until I find a NULL.

Is there a more efficient way that is still simple? I'm expecting strings less than 64 chars in length, but they could be longer.

theschmitzer
  • 12,190
  • 11
  • 41
  • 49

2 Answers2

5

I think reading byte by byte is a reasonable option, but I would use a BufferedStream to minimize IO on the underlying stream.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
2

Read the whole thing into a string, then use String.Split to split on the nulls.

Remember that strings are not null-terminated in .NET.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 2
    He didn't say how long the whole message was (or even whether it terminates). There's a good chance reading the whole thing is not feasible. – Matthew Flaschen May 05 '09 at 22:03
  • 1
    On the other hand there is not enough info in the question to be sure it is not feasible. It is a good idea that may well help someone else if no the OP. – ScottS May 05 '09 at 22:29
  • Actually there kind of is enough information if you read between the lines. He mentions he is obtaining the input data from a network connection, which implies using winpcap or simliar API. Therefore as Matthew said it is highly probable that he is pulling input from a continious stream of data. – Anonymous Type Dec 29 '10 at 21:37