Re: Reading 8 bit chars off a stream

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I assume you are using the SerialPort class in the System.IO.Ports
namespace. If not, you might want to look at that.

You might want to consider a MemoryStream instead of a List<byte> to
store the bytes. A MemoryStream will store the bytes as well and allocate
new memory when you need it, but if the message is large, then you can call
the GetBuffer method on the stream to get the internal buffer, whereas with
List<byte> you have to create a new array, and that might be expensive.

Once you have the byte array, you can pass it to the GetString method on
the encoding in one shot, passing the array, as well as the start index and
number of characters to process. This will help in the case of using the
MemoryStream, since you can get the buffer directly and then tell the
encoder just how much to process.

It would be nice if you could use the StreamReader here to perform the
encoding for you, but unfortunately, it buffers the characters from the
stream internally, and if the device isn't going to offer up anything past
the message that it sent, the StreamReader will hang.

Hope this helps.



<cartoper@xxxxxxxxx> wrote in message
news:1139513138.526638.33730@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I am getting a packet of data from an instrument. The strings in the
data packet are null terminted. What is the best way to extract a
string from the data packet?

I am working in .Net 20.

Presently the code reads byte by byte through the packet adding the
value into a List<byte>. The a new byte[] is created based on the
count of the List<byte>. The code iterates though the List<byte> to
populate the byte[]. Finally the byte[] is passed to an instance of
Encoding to get the string.

Is there a better way which does not require looping thourgh the string
twice?



.



Relevant Pages

  • Re: reading XML string and writing it to a SQL database
    ... My goal is to parse the data packet and write the data to a MS/SQL database. ... XML stream to a SQL table in a safe simple fashion. ... I retrieve the data from the scale via telnet as a text string. ...
    (borland.public.delphi.database.ado)
  • Re: DES Key & IV
    ... public String EncryptData(String data, ... string strResult; //Return Result ... // mOut is the output stream. ... MemoryStream mStream = new MemoryStream; ...
    (microsoft.public.dotnet.security)
  • Re: XslCompiledTransform Load method getting "Root element is missing" XSL Compile error...???
    ... MemoryStream outputStream = new MemoryStream; ... //write the string buffer to the stream ... If you have a string with XML then simply use a StringReader over that string and pass the StringReader or XmlReader over that StringReader to the method e.g. ...
    (microsoft.public.dotnet.xml)
  • Re: Problem reading an XML string without first writing to disk
    ... I was playing with MemoryStream but unable to get it to work... ... rptds.ReadXml wants a file or a stream. ... From an array of byte. ... array of byte from a string? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Basic Object Serialization to a STRING
    ... MemoryStream m = new MemoryStream; ... serialize object into stream. ... Call Convert.ToBase64which will return you a string that is ... Deserialize your object from your stream. ...
    (microsoft.public.dotnet.framework)