Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BinaryReader.ReadChars() advancing position too far

Status
Not open for further replies.

Sypher2

Programmer
Oct 3, 2001
160
US
I have a BinaryReader that I am parsing data with. There are two elements included in the data that are 6 bytes of ASCII (null-terminated).

Within the code I have:

Code:
For i = 0 to 10
   Dim tNum as integer = br.ReadInt32
   Dim tConn as byte = br.ReadByte
   Dim tLo as string = br.ReadChars(6)
   Dim tHi as string = br.ReadChars(6)
   Dim tImg as integer = br.ReadInt32
Next

This works for as long as the data is empty (zeros) and the BinaryReader's underlying stream position is advancing as it should. Once I hit an iteration with real data, the ReadChars will sometimes advance the stream 7 or 10 bytes instead of 6. Then the remaining iterations are off.

Does anyone know why this is happening? Is the null-terminator causing a problem? Why would the stream advance past the point I asked for?

Thanks.

 
I figured it out.

I needed to create the BinaryReader with the correct encoding...

Code:
Dim br As New BinaryReader(stream, System.Text.Encoding.ASCII)

That gives one byte per char.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top