Binary Reader &*($%^%$ !!!

J

james

My head is about to explode!! I am trying to read the header of a Dataflex file and for whatever reason, using Binary Reader, I
am getting some strange results. Here is some code:

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click

Dim FilePath As String

Dim s As String

Dim tblName As Long = &H2D0 ' Table Name starts here

Dim numfields As Long = &HA5 ' Number of FIELDS starts here

Dim reclength As Long = &H9A 'rec length starts at 9A to 9B

Dim totalRecords As Long = &H8 ' Goes from Hex:08 to Hex:0B

Dim fieldtype As Object 'field types can be 00=ASCII,01=Numeric,02=DATE,03=Overlap

'Display Open dialog box and select text file

OpenFileDialog1.Filter = "DAT Files(*.DAT)|*.dat|Tag Files(*.TAG)|*.TAG|CFG Files(*.CFG)|*.CFG|HDR Files(*.HDR)|*.HDR|All
Files(*.*)|*.*"

OpenFileDialog1.ShowDialog()

'If Cancel button not selected, load FilePath variable

If OpenFileDialog1.FileName <> "" Then

FilePath = OpenFileDialog1.FileName

Try

Me.Cursor = Cursors.WaitCursor

Dim i As Integer


Dim fs As FileStream

fs = New FileStream(FilePath, FileMode.Open, FileAccess.Read)

Dim r As StreamReader = New StreamReader(fs)

Dim sz As Integer = fs.Length

Dim mybytearray() As Byte = New Byte(sz) {}

Dim myencoding As ASCIIEncoding = New ASCIIEncoding

Dim a As Object

Dim b As String

Dim amt As Integer ' first byte of records

Dim nxt As Integer 'second half of Num of records

'gets number of records

r.BaseStream.Seek(totalRecords, SeekOrigin.Begin)

r.BaseStream.Read(mybytearray, 0, 1)

a = myencoding.GetString(mybytearray)



ListBox1.Items.Add("Total Records: " + a.ToString)

'gets field length

r.BaseStream.Seek(reclength, SeekOrigin.Begin)

r.BaseStream.Read(mybytearray, 0, 1)

a = myencoding.GetString(mybytearray)

amt = Asc(a)

r.BaseStream.Read(mybytearray, 0, 1)

b = myencoding.GetString(mybytearray)

nxt = Asc(b)

If Asc(b) = 0 Then

nxt = Val(b)

Else

End If

ListBox1.Items.Add("Length of Records: " + amt.ToString + nxt.ToString)

r.BaseStream.Seek(numfields, SeekOrigin.Begin)

r.BaseStream.Read(mybytearray, 0, 2)

a = myencoding.GetString(mybytearray)

ListBox1.Items.Add("Number of Fields: " + Asc(a).ToString)

'Gets TableName

r.BaseStream.Seek(tblName, SeekOrigin.Begin)

r.BaseStream.Read(mybytearray, 0, 8)

a = myencoding.GetString(mybytearray)

ListBox1.Items.Add("Table Name: " + a.ToString)

r.Close()

fs.Close()

Catch ex As Exception

'display error messages if they appear

MessageBox.Show(ex.Message)

End Try

End If

Me.Cursor = Cursors.Default

Me.Text = "BinaryFileReader " + OpenFileDialog1.FileName

End Sub

The problem I am having occurs in this section of code:



r.BaseStream.Seek(totalRecords, SeekOrigin.Begin)

r.BaseStream.Read(mybytearray, 0, 1)

a = myencoding.GetString(mybytearray)



ListBox1.Items.Add("Total Records: " + a.ToString)

With the file I am reading it should return the Decimal Value 208 ( Hex D0). This is at address H8 (totalRecords). Instead it
returns a: P (using a.ToString) or whenever I try to get the value it becomes 50 or 80 !! But, never 208 as it should be
looking at the header with a Hex Editor.

The other values in the file like, Number of Fields are correct. And, Record Length is almost right. I need to figure out how
to read the bytes from 9A to 9B and then SHIFT (LSB first) them to get the number correctly.

I have Googled and read MSDN and help until I am cross eyed (not easy for someone with only one good eye! )

Any suggestions or ideas would be most welcome.

james
 
J

james

Followup to my own post................
Just color me dumb!! I fixed the problem............(and actually started using the Binary Reader!!!)
james
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top