Troy,
You have two offline friends with vs2005. The first is the object browser... it's a little icon with pink in it at the top. It shows all of the classes, and their properties and methods.
system.bitconverter and system.text.convert are described in there.
The second is the HELP menu. Use the help menu. Look up binaryreader and file reader.
Let says that you have a 1000 byte file. you want to diddle with bytes the seventh byte as a byte the fiftieth byte as a short (Two bytes) and the and the hundredth as and integer (4 bytes). I remember that bytes arrays begin at the zeroith byte so these will all be off by one. Since you are a beginner I’d make constants describing offsets like this:
Const cSeventh as short = 7 – 1 ‘ I do this for two reasons – To document what I’ve and make the code readable in the future
Const cfiftieth as short = 50 -1
Const cOneHundreth as short = 100 – 1
First find out how to open a file and read it in bytes . I went to the object browser and and entered readbytes for the fun of it. This is what popped up:
System.IO.BinaryReader.ReadBytes(Integer) As Byte()
You can get brief information, there and more in Help. Googling that exact thread will also. But this time I went to Help and entered in Binaryreader and I find that I can read on the class and it has good examples:
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Class MyStream
Private Const FILE_NAME As String = "Test.data"
Public Shared Sub Main()
' Create the new, empty data file.
If File.Exists(FILE_NAME) Then
Console.WriteLine("{0} already exists!", FILE_NAME)
Return
End If
Dim fs As New FileStream(FILE_NAME, FileMode.CreateNew)
' Create the writer for data.
Dim w As New BinaryWriter(fs)
' Write data to Test.data.
Dim i As Integer
For i = 0 To 10
w.Write(CInt(i))
Next i
w.Close()
fs.Close()
' Create the reader for data.
fs = New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
Dim r As New BinaryReader(fs)
' Read data from Test.data.
For i = 0 To 10
Console.WriteLine(r.ReadInt32())
Next i
r.Close()
fs.Close()
End Sub
End Class
First of all the imports must be included at the top of your form which is a class.
Imports System
Imports System.IO ‘These bring classes and namespaces into your class (form)
Hmmmmm from thing I see I have a lot of options. Since this is going to be a sort quick program, I’m going to make functions for reading and a writing bytes to a file
They’ll look like this
Private Function ReadBytes(ByVal FileName As String, ByRef Buffer As Byte()) As Boolean
ReadBytes = False ' status return
If Not File.Exists(FileName) Then
MsgBox("File" + FileName + " doesn’t exist!", MsgBoxStyle.Information, "ReadBytes")
Exit Function
End If
Dim fi As New FileInfo(FileName)
Dim bytecount As Integer = fi.Length
If bytecount > MaxBufSize Then bytecount = MaxBufSize ' don't read any more than you need
If bytecount < cOneHundreth + 4 Then
MsgBox(FileName + "is smaller than minimum number of bytes", _
MsgBoxStyle.Information, "ReadBytes")
Exit Function
End If
ReDim Buffer(bytecount)
Try
Using fs As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Using r As New BinaryReader(fs)
Try
Buffer = r.ReadBytes(bytecount)
Catch e As Exception
MsgBox("Cannot Read " + FileName + " doesn’t exist!", MsgBoxStyle.Information, _
"ReadBytes" + e.Message)
Exit Function
Finally
r.Close() ' We're going to leave fs open
fs.Close()
fs.Dispose()
End Try
End Using
End Using
Catch e As Exception
MsgBox("Cannot Open " + FileName, MsgBoxStyle.Information, _
"ReadBytes" + e.Message)
Exit Function
End Try
Return True
End Function
When this completes.... your buffer is full of the necessary number of bytes.
Its companion routine would look like this:
Private Function WriteBytes(ByVal FileName As String, ByRef Buffer As Byte()) As Boolean
WriteBytes = False
Try
Using fs As New FileStream(FileName, FileMode.Open, FileAccess.Write)
Using w As New BinaryWriter(fs)
Try
w.Write(Buffer, 0, Buffer.Length - 1)
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Exclamation, "Error writing file")
Finally
w.Close() ' Impeach rather
End Try
w.Close()
End Using
fs.Close()
fs.Dispose()
End Using
Catch e As Exception
MsgBox("Cannot Open " + FileName, MsgBoxStyle.Information, _
"WriteBytes" + e.Message)
Exit Function
End Try
Return True
End Function
This is what you can do with HELP. It’s a good reference. When you don’t understand a Keyword like Using, it’s right there in help.
Ok so let’s do some mild bit twiddling …. And I’m going to leave the user interface up to you.
How do we get those bytes of concern into something readable
The first is a byte and we are already in bytes so
Here’s a main routine which is the event handler of a “Go” button
Private Sub cbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbGo.Click
Dim b() As Byte = Nothing
ReadBytes(fName, b)
TextBox1.Text = "Seventh Byte: " + Hex(b(cSeventh)) + vbCrLf
Dim fifthieth As UShort = System.BitConverter.ToUInt16(b, cfifthieth)
TextBox1.Text += "Fifthiest+ as Ushort: " + Hex(fifthieth) + vbCrLf
Dim Onehundreth As UInt32 = System.BitConverter.ToUInt32(b, Onehundreth)
TextBox1.Text += "Onehundreth+ as Uint32: " + Hex(Onehundreth) + vbCrLf
' I'm going to leave the rest to you
'do your user interface stuff
' and then write it out.
WriteBytes(fName, b)
End Sub
Here’s the whole program so far… enjoy
Imports System
Imports System.IO 'These bring classes and namespaces into your class (form)
Public Class Form1
Protected Const cSeventh As Short = 7 - 1 ' I do this for two reasons – To document what I’ve done
' and to make the code readable in the future
Protected Const cfifthieth As Short = 50 - 1
Protected Const cOneHundreth As Short = 100 - 1
Protected Const MaxBufSize As Short = 1000
Protected Const fName As String = "c:\temp\code.doc"
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Multiline = True
End Sub
Private Sub cbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbGo.Click
Dim b() As Byte = Nothing
ReadBytes(fName, b)
TextBox1.Text = "Seventh Byte: " + Hex(b(cSeventh)) + vbCrLf
Dim fifthieth As UShort = System.BitConverter.ToUInt16(b, cfifthieth)
TextBox1.Text += "Fifthiest+ as Ushort: " + Hex(fifthieth) + vbCrLf
Dim Onehundreth As UInt32 = System.BitConverter.ToUInt32(b, Onehundreth)
TextBox1.Text += "Onehundreth+ as Uint32: " + Hex(Onehundreth) + vbCrLf
' I'm going to leave the rest to you
'do your user interface stuff
' and then write it out.
WriteBytes(fName, b)
End Sub
Private Function ReadBytes(ByVal FileName As String, ByRef Buffer As Byte()) As Boolean
ReadBytes = False ' status return
If Not File.Exists(FileName) Then
MsgBox("File" + FileName + " doesn’t exist!", MsgBoxStyle.Information, "ReadBytes")
Exit Function
End If
Dim fi As New FileInfo(FileName)
Dim bytecount As Integer = fi.Length
If bytecount > MaxBufSize Then bytecount = MaxBufSize ' don't read any more than you need
If bytecount < cOneHundreth + 4 Then
MsgBox(FileName + "is smaller than minimum number of bytes", _
MsgBoxStyle.Information, "ReadBytes")
Exit Function
End If
ReDim Buffer(bytecount)
Try
Using fs As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Using r As New BinaryReader(fs)
Try
Buffer = r.ReadBytes(bytecount)
Catch e As Exception
MsgBox("Cannot Read " + FileName + " doesn’t exist!", MsgBoxStyle.Information, _
"ReadBytes" + e.Message)
Exit Function
Finally
r.Close() ' We're going to leave fs open
fs.Close()
fs.Dispose()
End Try
End Using
End Using
Catch e As Exception
MsgBox("Cannot Open " + FileName, MsgBoxStyle.Information, _
"ReadBytes" + e.Message)
Exit Function
End Try
Return True
End Function
Private Function WriteBytes(ByVal FileName As String, ByRef Buffer As Byte()) As Boolean
WriteBytes = False
Try
Using fs As New FileStream(FileName, FileMode.Open, FileAccess.Write)
Using w As New BinaryWriter(fs)
Try
w.Write(Buffer, 0, Buffer.Length - 1)
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Exclamation, "Error writing file")
Finally
w.Close() ' Impeach rather
End Try
w.Close()
End Using
fs.Close()
fs.Dispose()
End Using
Catch e As Exception
MsgBox("Cannot Open " + FileName, MsgBoxStyle.Information, _
"WriteBytes" + e.Message)
Exit Function
End Try
Return True
End Function
Private Sub cbExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbExit.Click
End
End Sub
End Class
Please note that the board editor can make one line appear to be two or more lines. This compiles and runs flawlessly. It’s really irritating for a new person who doesn’t know syntax to complain that there are compile errors. I hold you responsible for knowing syntax.
There is a test file in my directory c:\temp\ you’ll have to pick your own.
Oh Yes, I forgot… here’s the output from textbox1
Seventh Byte: 1A
Fifthiest+ as Ushort: 0
Onehundreth+ as Uint32: E011CFD0
Goodluck....