473,840 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BinaryReader.Pe ekChar ArgumentExcepti on: Conversion Buffer Overflow

Tim
Hi,

I'm experiencing some problem with the following code:

st = File.Open(sFile name, FileMode.Open, FileAccess.Read Write)
br = New BinaryReader(st )

Do Until br.PeekChar = -1
Dim buffer() As Byte = br.ReadBytes(10 24)
...
Loop

Basically, PeekChar would sometimes cause the System.Argument Exception in
mscorlib.dll with the message "Conversion buffer overflow." However, when I
examine the value after inserting breakpoints, br.PeekChar returns normal
values without error. Furthermore, the number of bytes read before the
error occurs is always the same, assuming I am using the same file.

This problem disappears after I change the code to the following:

Dim len As Integer = 1

Do Until len = 0
Dim buffer(1024) As Byte
len = br.Read(buffer, 0, 1024)
...
Loop

This makes me believe that the problem _is_ indeed caused by PeekChar.

I've tried searching for people expericing similar problems online, but to
no avail. Anyone has any idea?

Thanks.
Tim
Nov 20 '05 #1
5 7508
Hi Tim,

I've got no idea what the problem is but I'm intrigued by 'Furthermore,
the number of bytes read before the error occurs is always the same, assuming
I am using the same file.' which suggests that there's something in the file
at that point which is causing the error. This makes sense if it's a
conversion error.
The questions must be, therefore:
What is it converting? and
What is it trying to convert it to?

Time to go get some bytes out of that file. And make sure that you have
the right bytes! ;-)

Regards,
Fergus
Nov 20 '05 #2
Tim,
Remember a Char is 2 bytes. Depending on the encoding on the underlying
stream if you have an odd number of bytes in the file I would expect the
error!

I would use a variation of your second loop, as PeekChar is NOT the EOF
function.

Dim len As Integer
Do
Dim buffer(1023) As Byte
len = br.Read(buffer, 0, 1024)
Loop Until len = 0

Although your second loop effectively does the same thing.
Dim buffer(1024) As Byte BTW: You have an array of 1025 bytes, as the 1024 is the high index, the low
index is 0.

Hope this helps
Jay

"Tim" <ti********@yah oo.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. .. Hi,

I'm experiencing some problem with the following code:

st = File.Open(sFile name, FileMode.Open, FileAccess.Read Write)
br = New BinaryReader(st )

Do Until br.PeekChar = -1
Dim buffer() As Byte = br.ReadBytes(10 24)
...
Loop

Basically, PeekChar would sometimes cause the System.Argument Exception in
mscorlib.dll with the message "Conversion buffer overflow." However, when I examine the value after inserting breakpoints, br.PeekChar returns normal
values without error. Furthermore, the number of bytes read before the
error occurs is always the same, assuming I am using the same file.

This problem disappears after I change the code to the following:

Dim len As Integer = 1

Do Until len = 0
Dim buffer(1024) As Byte
len = br.Read(buffer, 0, 1024)
...
Loop

This makes me believe that the problem _is_ indeed caused by PeekChar.

I've tried searching for people expericing similar problems online, but to
no avail. Anyone has any idea?

Thanks.
Tim

Nov 20 '05 #3
Tim
Fergus,

I've looked into the content of the file at each point where the exception
occurs, and there appears to be absolutely no correlation between the bytes.
I'm not sure if VB is trying to do any kind of underlying conversion, but I
certainly am not :)
Tim

"Fergus Cooney" <fi******@tesco .net> wrote in message
news:u3******** ******@TK2MSFTN GP09.phx.gbl...
Hi Tim,

I've got no idea what the problem is but I'm intrigued by 'Furthermore,
the number of bytes read before the error occurs is always the same,
assuming
I am using the same file.' which suggests that there's something in the file
at that point which is causing the error. This makes sense if it's a
conversion error.
The questions must be, therefore:
What is it converting? and
What is it trying to convert it to?

Time to go get some bytes out of that file. And make sure that you have
the right bytes! ;-)

Regards,
Fergus

Nov 20 '05 #4
Tim
Jay,

The exceptions are occurring in the middle of the file (almost never at the
end), so I'm going to assume that sizeof(char) is not the issue.

I guess I was mislead by the book "Programmin g Microsoft Visual Basic .NET
Core Reference" by Francesco Balena, in which the following example is used:

Dim st2 as Stream = File.Open("c:\v alue.dat", FileMode.Open,
FileAccess.Read )
Dim br2 as New BinaryReader(st 2)

Do Until br2.PeekChar = -1
Console.WriteLi ne(br2.ReadDoub le)
Loop

br2.Close()
st2.Close()

But then again, since sizeof(double) % sizeof(char) == 0, I guess that's why
it works.

Thanks a lot for the help. I'll go ahead and use the second version.
Tim
Nov 20 '05 #5
Tim,
And what about Encoding itself?

Even if you are not at the end of the file, I understand that PeekChar needs
to decode the bytes into a Char. Based on the Encoding given (or not given)
when you open the Stream & BinaryReader that Encoding will attempt to
convert one or more bytes into a Char, if you just happen to be at a byte
that 'requires' four bytes to decode, but only have two bytes that make
sense... I would expect an Exception.

Hence I would not use PeekChar to check for EOF.

Remember the default encoding used on BinaryReader is UTF8Encoding.

Hope this helps
Jay

"Tim" <ti********@yah oo.com> wrote in message
news:O1******** ******@TK2MSFTN GP09.phx.gbl...
Jay,

The exceptions are occurring in the middle of the file (almost never at the end), so I'm going to assume that sizeof(char) is not the issue.

I guess I was mislead by the book "Programmin g Microsoft Visual Basic .NET
Core Reference" by Francesco Balena, in which the following example is used:
Dim st2 as Stream = File.Open("c:\v alue.dat", FileMode.Open,
FileAccess.Read )
Dim br2 as New BinaryReader(st 2)

Do Until br2.PeekChar = -1
Console.WriteLi ne(br2.ReadDoub le)
Loop

br2.Close()
st2.Close()

But then again, since sizeof(double) % sizeof(char) == 0, I guess that's why it works.

Thanks a lot for the help. I'll go ahead and use the second version.
Tim

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
341
by: Supra | last post by:
i saved file in binary. i saved 16 colours.. but how do i read back values into listbox? Dim st2 As Stream = File.Open _ ("C:\Documents and Settings\My Documents\Visual Studio Projects\VbNet\project\Write_Ini_File\bin\colours.dat", FileMode.Open, FileAccess.Read) Dim br2 As New BinaryReader(st2) Do Until br2.PeekChar = -1 ListBox1.Items.Add(br2.ReadByte) ==>this is not what i wanted
23
8768
by: Matt Garman | last post by:
Is there a clean, portable way to determine the maximum value of converted numerical fields with printf()-like functions? Doing this at compile-time would be preferable. For example, %i should never convert to a string that is longer than the number of digits in INT_MAX, right? I'd like to create a structure that contains character representations of numerical types (e.g., instead of storing 3421 as an int, it would store the char...
2
23627
by: Bob Rock | last post by:
I already found an alternative way to accomplish this (using ReadBytes), still I'd like to understand why I'm getting and error reading a text file using the following method. The exception is returned on the ReadString call. public string BinaryRead(string fileName) { FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
4
11938
by: Paul Steele | last post by:
I am writing a client/server application that communicates over tcp. The code I use to initiate the connection is as follows: NetworkStream networkStream = new NetworkStream(ClientSocket); toClient = new BinaryWriter(networkStream); fromClient = new BinaryReader(networkStream); There is similar code on the client side. This set up works fine and communication does occur. However, the communication is very slow. This is especially...
1
4802
by: Vitaly | last post by:
// Open input file and create the BinaryReader. br = new BinaryReader(new FileStream("Test.dat", FileMode.Open, FileAccess.Read)); // Read binary data. d = br.ReadDouble(); A question is how to do the same but read from standard input stream … like Console.In.
6
2182
by: Anil Gupte | last post by:
Here is my code: Dim fsReadStream As New FileStream(L3FileName, FileMode.Open, FileAccess.Read) Dim brReader As New BinaryReader(fsReadStream) Dim ByteArray() As Byte While brReader.PeekChar() -1 ByteArray = brReader.ReadBytes(1) End While
28
3422
by: James Brown | last post by:
All, I have a series of characters which I need to convert to integer values. Each character is read in turn from a function 'nextch', and hex-digits are identified by the isxdigit function - so I'm looking at '0' - '9', 'A' - 'Z' and 'a' - 'z'. Here is what I've got: int num = 0;
9
8821
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this buffer size as the number of characters to read in using the function read(). The issue I am having is read() expects that the value for the number of characters to read-in will be of type std::streamsize, which is apparently signed int. My buffer
9
5511
by: Drayshak | last post by:
Hi, I'm currently making an application which reads a file and extracts information from it. Whole files are embedded in this single file, and information about each file that is embedded is stored at a specific location within this file. fs = new FileStream(@"File Location", FileMode.Open); br = new BinaryReader(fs); rtbOutput.AppendText("English Title: " + getLanguage(br)); public string getLanguage(BinaryReader reader) { ...
0
9856
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10657
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9436
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7836
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.