473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
7 3406
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
Good hint, thanks. Now the question, how to write string in stream in ANSI
(single-byte) format?
Nov 16 '05 #3
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Shane | last post by:
Hi, Thanks in advance for the help. I have been to many websites and tried several solutions to my problem, but have fixed part of it. It's time to come humbly to the newsgroups for help :-) ...
5
by: Patrick De Ridder | last post by:
The following code produces an error on the very last line, namely: An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
1
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
0
by: Harley | last post by:
Hello, I am just learning the tcp/ip functions etc under vb.net so please look over me if this is obviouse. I have been all over looking into any functions that I didn't totaly understand and...
11
by: Sehboo | last post by:
Hi, I want to write a very very simple mail application which my father can use (maybe then I will be able to send and receive emails from him). This application will have a text box, where he...
2
by: iwdu15 | last post by:
hey, i was wondering if 1) anyone could tell me whats wrong with my code, i did a little fixing to the msdn version fo this, or 2) how to make a simple program that will connect and listen for...
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
5
by: Yossarian | last post by:
I have a handheld running CE .NET 4.2 and I am using c# with framework 1.1 to develop a solution for syncing data that is on the handheld with the local pc. Our handheld cradles only support...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.