By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,481 Members | 3,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,481 IT Pros & Developers. It's quick & easy.

Simple question. How to read/write from/into stream.

P: n/a
Hi,

I have an integer value I need to read/write as 4 bytes from/into Stream
(NetworkStream), but I cannot find ReadInt/WriteInt method in the Stream
class.
The same problem with string.

Thanks in advance
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Use the System.IO.BinaryWriter class.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"John Demigor" <de*****@hotmail.com> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Hi,

I have an integer value I need to read/write as 4 bytes from/into Stream
(NetworkStream), but I cannot find ReadInt/WriteInt method in the Stream
class.
The same problem with string.

Thanks in advance

Nov 16 '05 #2

P: n/a
Good hint, thanks. Now the question, how to write string in stream in ANSI
(single-byte) format?
Nov 16 '05 #3

P: n/a
You should use the Write overloaded method of signature:
Write ( byte [] data, int start, int length )
To that method you should supply a byte array which you have created by
calling
System.Text.Encoding.[YOUR_PREFERRED_ENCODING].GetBytes("Your string");

So, something like:
string text = "Happy weekend!";
BinaryWriter bw...;
byte [] data = System.Text.Encoding.ASCII.GetBytes(text);
bw.Write(data, 0x0, data.Length);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"John Demigor" <de*****@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
Good hint, thanks. Now the question, how to write string in stream in ANSI
(single-byte) format?

Nov 16 '05 #4

P: n/a
John Demigor <de*****@hotmail.com> wrote:
I have an integer value I need to read/write as 4 bytes from/into Stream
(NetworkStream), but I cannot find ReadInt/WriteInt method in the Stream
class.
Use a BinaryWriter.
The same problem with string.


Again, you can use BinaryWriter - but be careful of Write(string) as
this adds an encoded version of the string length before the string
itself. Use Encoding.GetBytes to get the bytes and write them if you
need to. Alternatively, if this is in a different stream, construct a
StreamWriter which is designed for writing text.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
Thanks,
System.Text.Encoding.[YOUR_PREFERRED_ENCODING].GetBytes("Your string");


My preferred encoding is usually ISO-8859-1, but it would be great to use
windows system codepage.
How does it go then?
Nov 16 '05 #6

P: n/a
You should look at the GetEncoding method if you want some other encoding
than
those that are predefined.
Have a look at:
http://msdn.microsoft.com/library/de...dingtopic2.asp

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"John Demigor" <de*****@hotmail.com> wrote in message
news:O6*************@TK2MSFTNGP11.phx.gbl...
Thanks,
System.Text.Encoding.[YOUR_PREFERRED_ENCODING].GetBytes("Your string");


My preferred encoding is usually ISO-8859-1, but it would be great to use
windows system codepage.
How does it go then?

Nov 16 '05 #7

P: n/a
John Demigor <de*****@hotmail.com> wrote:
System.Text.Encoding.[YOUR_PREFERRED_ENCODING].GetBytes("Your string");


My preferred encoding is usually ISO-8859-1, but it would be great to use
windows system codepage.
How does it go then?


Use Encoding.Default - and note that it's *not* the default for things
like StreamReader if you don't specify the encoding. It should really
be called something like Encoding.WindowsSystemDefault.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.