|
-
Sep 15th, 2008, 03:16 PM
#1
Thread Starter
Addicted Member
[RESOLVED] How Do You Read Data From An Offset?
I need to read in data from a file at offset B0, how do I do it?
What I'm trying to do is find a certain ASCII string at this offset.
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim loadMem As Array
Dim fileOpen = IO.File.Open(TextBox1.Text, IO.FileMode.Open)
Dim readOffset = fileOpen.BeginRead(loadMem, offset:=B0, numBytes:=4)
End Sub
I have that code, but 'B0' is underlined. I also have a sneaky feeling this wouldn't work anyway?
If anybody could help with this one, that'd be absolutely fantastic!
-
Sep 15th, 2008, 04:14 PM
#2
Re: How Do You Read Data From An Offset?
B0 is Hexdecimal. You just need to convert it to an Integer. Use "&HB0" instead. The &H part tells VB.NET that "Hey, this is Hexdecimal!" Also, it most likely won't work. You need two more parameters to BeginRead()
-
Sep 15th, 2008, 04:22 PM
#3
Thread Starter
Addicted Member
Re: How Do You Read Data From An Offset?
Thank you for your reply! You saved me a lot of time goofing about with my code!
Well, seeing as it wouldn't work, does anybody know how to read data from an offset?
-
Sep 15th, 2008, 04:30 PM
#4
Re: How Do You Read Data From An Offset?
Try something like this... it's easier 
Code:
'I'm reading 4 bytes, so I make an array of Bytes (0 to 3)
Dim loadMem(3) As Byte
'Open Filestream.
Dim fileOpen As IO.FileStream = IO.File.Open(TextBox1.Text, IO.FileMode.Open)
'Go to my offset.
fileOpen.Seek(&HB0, IO.SeekOrigin.Begin)
'Read 4 bytes into loadMem.
fileOpen.Read(loadMem, 0, 4)
'Close my Filestream/
fileOpen.Close()
'Show my output.
For i As Integer = 0 To 3
MsgBox(Chr(loadMem(i)))
Next
-
Sep 15th, 2008, 05:03 PM
#5
Thread Starter
Addicted Member
Re: How Do You Read Data From An Offset?
Thank you very very very much! This is nearly exactly what I'm looking for! Just one more question, how do I stick it all in one single message box?
-
Sep 16th, 2008, 07:37 AM
#6
Re: How Do You Read Data From An Offset?
You just need to turn your array of bytes into a string. You'll love this. .NET has it already built in for you in System.Text.Encoding
Here, I'm specifying UTF8 (Unicode) though you can use ASCII or whatever if this doesn't work for you.
Code:
MsgBox(System.Text.Encoding.UTF8.GetString(loadMem))
Final example of reading a string in a file from an offset:
Code:
'I'm reading 4 bytes, so I make an array of Bytes (0 to 3)
Dim loadMem(3) As Byte
'Open Filestream.
Dim fileOpen As IO.FileStream = IO.File.Open(TextBox1.Text, IO.FileMode.Open)
'Go to my offset.
fileOpen.Seek(&HB0, IO.SeekOrigin.Begin)
'Read 4 bytes into loadMem.
fileOpen.Read(loadMem, 0, 4)
'Close my Filestream/
fileOpen.Close()
'Show my output.
MsgBox(System.Text.Encoding.UTF8.GetString(loadMem))
-
Sep 16th, 2008, 01:54 PM
#7
Thread Starter
Addicted Member
Re: How Do You Read Data From An Offset?
Thank you so much Jenner! I have the part 100% working now! Again, thanks! You're an absolutely awesome developer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|