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

2 Questions on Reading Binary File; "Conversion buffer overflow"

P: n/a
Hi All,

What is the proper way to read a binary file into a byte[]?
I am using BinaryReader to read from a Stream and call the ReadByte method
of the BinaryReader object.

The method I'm using leads to the second question. I got the "Conversion
buffer overflow" error when I run the following code:
Stream s = openFileDialog1.OpenFile();

System.IO.BinaryReader br = new BinaryReader(s);

byte[] ba = new byte[s.Length]; // s.Length ~= 20000

while (br.PeekChar() > -1) {

ba[s.Position] = br.ReadByte(); // <- s.Position ~= 400 when error
occurred

}

Thanks in advance


Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Try the following:

byte[] bytes = new byte[stream.Length];

stream.Position = 0;
stream.Read(bytes, 0, bytes.Length);
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"poifull" <po*******@yahoo.com> wrote in message news:kI*******************@tornado.texas.rr.com...
Hi All,

What is the proper way to read a binary file into a byte[]?
I am using BinaryReader to read from a Stream and call the ReadByte method of the BinaryReader object.

The method I'm using leads to the second question. I got the "Conversion buffer overflow" error when I run the following code:
Stream s = openFileDialog1.OpenFile();

System.IO.BinaryReader br = new BinaryReader(s);

byte[] ba = new byte[s.Length]; // s.Length ~= 20000

while (br.PeekChar() > -1) {

ba[s.Position] = br.ReadByte(); // <- s.Position ~= 400 when error occurred

}

Thanks in advance

Nov 17 '05 #2

P: n/a
Hi,
Take a look at this code:
int readed=0;
byte[] buff = new Byte[2048];
FileStream fstream = new FileStream( filename, FileMode.Open);

while( (readed=fstream.Read( buff, 0, 2048))>0 )
networkstream.Write( buff, 0, readed);
fstream.Close();
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"poifull" <po*******@yahoo.com> wrote in message
news:kI*******************@tornado.texas.rr.com...
Hi All,

What is the proper way to read a binary file into a byte[]?
I am using BinaryReader to read from a Stream and call the ReadByte method
of the BinaryReader object.

The method I'm using leads to the second question. I got the "Conversion
buffer overflow" error when I run the following code:
Stream s = openFileDialog1.OpenFile();

System.IO.BinaryReader br = new BinaryReader(s);

byte[] ba = new byte[s.Length]; // s.Length ~= 20000

while (br.PeekChar() > -1) {

ba[s.Position] = br.ReadByte(); // <- s.Position ~= 400 when
error occurred

}

Thanks in advance

Nov 17 '05 #3

P: n/a
Dave <NO*********@dotcomdatasolutions.com> wrote:
byte[] bytes = new byte[stream.Length];

stream.Position = 0;
stream.Read(bytes, 0, bytes.Length);


As it stands, that's not good code - you should *always* test the
result of stream.Read, as you may not be able to read everything in one
chunk.

See http://www.pobox.com/~skeet/csharp/readbinary.html

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

This discussion thread is closed

Replies have been disabled for this discussion.