Help needed with reading binary files

dd86

Member
Joined
Oct 14, 2005
Messages
16
Programming Experience
Beginner
I need help with binary file access. Apparently all the information in it is in hexadecimal. I need to extract that information as hexadecimal and push it into an array. Can anyone help me with this?
 
does anyone knw hw to create an array without bounds? 'cos i won' knw the no. of bytes tht will b in the file...
 
First define the array as
Dim buffer() as Bytes
then
Redim buffer(size)

or else

Dim buffer() As Byte
buffer = New Byte(size) {}

BTW, can't you guys just search the web or MSDN for this kind of simple things, instead of always posting and waiting someone else to come and answer. These are language fundamentals and that is why MSDN is there

 
If you want to preserve the array and expand then
Redim Preserve buffer(newSize)

OR else use ArrayList object
Why do u say u don't know the no of bytes, it should be the file size in bytes
 
i don' think so...i hve a file thts 1 KB in size and only has 131 bytes in it...BTW wen i say i'm a vb.net newbie, i really mean it...i hve no proper foundation in vb.net & was given a limited time to get familiar with it
 
i am creating an array that will store the hexadecimal values...this is my code....i receive a IndexOutOfRange Exception

Dim num As Byte
Dim n As String
Dim endOFStream As Boolean
Dim buffer() As String
buffer = New String() {}
Dim s As Integer = 0

Dim r As System.IO.BinaryReader = New System.IO.BinaryReader(fs)


While (Not endOFStream)
'num = r.ReadUInt16
Try
num = r.ReadByte()
n = Hex(CInt(num))
buffer(s) = n
s = s + 1
'Dim writer As IO.StreamWriter
'writer = IO.File.AppendText("c:\myfile.txt")
'writer.WriteLine(n)
'writer.Close()
Catch ex As EndOfStreamException
endOFStream = True
End Try
End While
MessageBox.Show(buffer.Length)
End Sub

really sorry but i really dunno much abt arrays tht hve unlimited spaces in vb.net...experimented before but failed
 
updated code

this is my new code...almost near completion but i dunno why the last If loop in the button click event is nt workin properly.its supposed to write stuff 17 times...its jus doin it once

Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form

Public oXL As Excel.Application
Public oWB As Excel.Workbook
Public oSheet As Excel.Worksheet
Public oRng As Excel.Range

Dim buffer As New ArrayList
Dim nom As Integer = 27
Dim min As Integer = 28

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(112, 192)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 272)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim fs As FileStream = New FileStream("C:\Arch.bin", FileMode.OpenOrCreate)
Dim num As Byte
Dim n As String
Dim endOfStream As Boolean

Dim r As System.IO.BinaryReader = New System.IO.BinaryReader(fs)

While (Not endOfStream)

Try
num = r.ReadByte()
n = Hex(CInt(num))

If n = "20" Then
n = "00"
buffer.Add(n)
ElseIf n.Length = 1 Then
n = "0" + n
buffer.Add(n)
Else
buffer.Add(n)
End If

Catch ex As EndOfStreamException
endOfStream = True
End Try

End While

If buffer.Item(4) = "00" Then
buffer.Item(4) = "20"
End If

Call LoadExcel()
Dim row As Integer = 2
If nom <> buffer.Count Then
If (min - 28) / 6 <> CInt("&H" & buffer.Item(nom)) Then
Call WriteXL(row, min)

row = row + 1
min = min + 6
Else

End If
Else
End If

End Sub

Sub LoadExcel()
oXL = CreateObject("Excel.Application")
oXL.Visible = True

' Get a new workbook.
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet

' Start Excel and get Application object.
' Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "Record header"
oSheet.Cells(1, 2).Value = "Finger header"
oSheet.Cells(1, 3).Value = "Minutia type + X"
oSheet.Cells(1, 4).value = "Y"
oSheet.Cells(1, 5).value = "Angle"
oSheet.Cells(1, 6).Value = "Image quality"
oSheet.Cells(1, 7).value = "Private Data Area"
oSheet.Cells(2, 1).Value = "0x" + buffer.Item(0) + buffer.Item(1) + buffer.Item(2) + buffer.Item(3) + buffer.Item(4) + buffer.Item(5) + buffer.Item(6) + buffer.Item(7) + buffer.Item(8) + buffer.Item(9) + buffer.Item(10) + buffer.Item(11) + buffer.Item(12) + buffer.Item(13) + buffer.Item(14) + buffer.Item(15) + buffer.Item(16) + buffer.Item(17) + buffer.Item(18) + buffer.Item(19) + buffer.Item(20) + buffer.Item(21) + buffer.Item(22) + buffer.Item(23)
oSheet.Cells(2, 2).value = "0x" + buffer.Item(24) + buffer.Item(25) + buffer.Item(26) + buffer.Item(27)
End Sub

Sub WriteXL(ByVal row As Integer, ByVal min As Integer)
oSheet.Range("C" & row).Value = "0x" + buffer.Item(min) + buffer.Item(min + 1)
oSheet.Range("D" & row).Value = "0x" + buffer.Item(min + 2) + buffer.Item(min + 3)
oSheet.Range("E" & row).Value = "0x" + buffer.Item(min + 4)
oSheet.Range("F" & row).Value = "0x" + buffer.Item(min + 5)
End Sub
End Class
 
This is the loop I need help with. CInt("&H" & buffer.Item(nom)) has a value of 17 in this case.

If nom <> buffer.Count Then
If (min - 28) / 6 <> CInt("&H" & buffer.Item(nom)) Then
Call WriteXL(row, min)

row = row + 1
min = min + 6
Else

End If
Else
End If

Please help me out as i need to finish this before Tuesday. Thanks.
 
Back
Top