Results 1 to 7 of 7

Thread: [RESOLVED] How Do You Read Data From An Offset?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Resolved [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!

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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()
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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?

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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?

  6. #6
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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))
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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
  •  



Click Here to Expand Forum to Full Width