473,324 Members | 2,531 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,324 software developers and data experts.

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

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
3 2570
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Miki Tebeka | last post by:
Hello All, I'm shipping an application using py2exe. With Python2.3 it worked fine but when switching to Python2.4 I started getting "warning: string/unicode conversion" all over the place. ...
4
by: Spook | last post by:
Running a particular Access report or the query associated with it brings up the warning "Overflow". What does this mean? WhatI have I done wrong? Both report and query were fine before lunch...
14
by: TTroy | last post by:
Hello, can anyone explain why the following function will not work for INT_MIN: /* itoa: convert n to characters in s */ void itoa(int n, char s) { int i, sign; if((sign = n) < 0) /*...
7
by: Eric Laberge | last post by:
Aloha! This question is meant to be about C99 and unnamed compound objects. As I read, if such a construct as int *p = (int){0}; is used within a function, then it has "automatic storage...
3
by: SheldonMopes | last post by:
I sometimes get a pop-up box that reads "Overflow" and the module that is executing pauses. It doesn't get caught by my error trapping, and it seems to be randow. By random, I mean usually in the...
2
by: SAL | last post by:
I have the following line of code in my Page_Load Event of my ASP.net page: txtExplanationofChange.Attributes.Add ("style","overflow :hidden"); which allows me to can turn off the Scrollbar of my...
5
by: arnuld | last post by:
this is from mentioned section. i did not understand some things here: it means "flushing the buffer" and "writing to output device" are SAME thing, these are just 2 different names for the...
0
by: snowdream1982 | last post by:
Dear all, I hit an error OVERFLOW but when I insert the sql statement below manually in the SQLEditor, it goes well, no errors at all. My coding: SQLInsertTempMf = "INSERT INTO tempmf " &...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.