Transferring data from a MemoryStream to a string

C

Claire

I've read some data from the serial port into a memorystream object.
Bytes 0..3 of the memorystream data are binary data, the remainder is a
unicode string.
How do I transfer this data to an instance of a string please?

Ive tried the following (where s is a memorystream)

s.Position = 4;
tr = new System.IO.StreamReader(s);
textBox1.Text = textBox1.Text + tr.ReadToEnd();
 
J

Jon Skeet [C# MVP]

Claire said:
I've read some data from the serial port into a memorystream object.
Bytes 0..3 of the memorystream data are binary data, the remainder is a
unicode string.
How do I transfer this data to an instance of a string please?

Ive tried the following (where s is a memorystream)

s.Position = 4;
tr = new System.IO.StreamReader(s);
textBox1.Text = textBox1.Text + tr.ReadToEnd();

That should be fine, except you haven't specified an Encoding to use.
If by "a unicode string" you mean it's in UTF-16, try just specifying
Encoding.Unicode or Encoding.BigEndianUnicode depending on the byte
ordering.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top