none
Write Hex File RRS feed

  • Question

  • hello, sorry if I much asking, my question is almost like this : http://social.msdn.microsoft.com/Forums/en-SG/vbgeneral/thread/a2a2cd8b-f09c-41f1-be09-c3f4259c9727

    I want program to write on offset 108, I was used Filestream and Binary Reader to read data:

    Using fs As New FileStream(Openfiledialog1.filename, FileMode.Open)
        Using rdr As New BinaryReader(fs)
         Dim seeker As String = fs.Seek(108, SeekOrigin.Begin)
         Dim str() As Byte = rdr.ReadBytes(32)
         Dim title As String = Encoding.UTF8.GetString(str)
         Textbox1.Text = title
        End Using
       End Using
    

    I don't have any idea to change data on offset 108, I read with ReadBytes(32) please help me, sorry If my English so bad, thanks!

    Sunday, August 14, 2011 3:58 PM

Answers

  •   Dim bytes As Byte()
    
      Private Sub SetByteInArray(ByVal data As Byte, ByVal index As Integer)
        bytes(index) = data
      End Sub
    
      Private Sub ReadBytesFromFile(ByVal fileName As String)
        Using stream As New FileStream(fileName, FileMode.Open, FileAccess.ReadWrite)
          Using reader As New BinaryReader(stream)
            Me.bytes = reader.ReadBytes(stream.Length)
          End Using
        End Using
      End Sub
    
      Private Sub WriteBytesToFile(ByVal fileName As String)
        Using stream As New FileStream(fileName, FileMode.Open, FileAccess.ReadWrite)
          Using writer As New BinaryWriter(stream)
            writer.Write(bytes)
            writer.Flush()
          End Using
        End Using
      End Sub
    



    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    Sunday, August 14, 2011 5:47 PM


  • I don't have any idea to change data on offset 108, I read with ReadBytes(32) please help me, sorry If my English so bad, thanks!

     

    Hi,

    I guess you want to change the 108th BYTE in a file?

    If so, add one Button to a Form and try this please:>>

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim fileBytes() As Byte = Nothing
        Dim fileName As String = ""
        Dim fileLength As Long
        Dim dlgResult As DialogResult
    
        Using OpenFileDialog1 As New OpenFileDialog
          OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    
          dlgResult = OpenFileDialog1.ShowDialog
          If dlgResult = Windows.Forms.DialogResult.OK And OpenFileDialog1.FileName <> String.Empty Then
    
            Using fs As New FileStream(OpenFileDialog1.FileName, FileMode.Open)
              Using rdr As New BinaryReader(fs)
                fileLength = My.Computer.FileSystem.GetFileInfo(OpenFileDialog1.FileName).Length
                fileBytes = rdr.ReadBytes(CInt(fileLength))
              End Using
            End Using
    
            'Byte 108 will be at array position 107 as arrays start at index number = 0
            fileBytes(107) = &H3F '<< this is HEX value 3F
            My.Computer.FileSystem.WriteAllBytes(fileName, fileBytes, False)
    
          End If
        End Using
    
      End Sub
    
    End Class


     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Or see
    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Sunday, August 14, 2011 5:52 PM

All replies

  •   Dim bytes As Byte()
    
      Private Sub SetByteInArray(ByVal data As Byte, ByVal index As Integer)
        bytes(index) = data
      End Sub
    
      Private Sub ReadBytesFromFile(ByVal fileName As String)
        Using stream As New FileStream(fileName, FileMode.Open, FileAccess.ReadWrite)
          Using reader As New BinaryReader(stream)
            Me.bytes = reader.ReadBytes(stream.Length)
          End Using
        End Using
      End Sub
    
      Private Sub WriteBytesToFile(ByVal fileName As String)
        Using stream As New FileStream(fileName, FileMode.Open, FileAccess.ReadWrite)
          Using writer As New BinaryWriter(stream)
            writer.Write(bytes)
            writer.Flush()
          End Using
        End Using
      End Sub
    



    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    Sunday, August 14, 2011 5:47 PM


  • I don't have any idea to change data on offset 108, I read with ReadBytes(32) please help me, sorry If my English so bad, thanks!

     

    Hi,

    I guess you want to change the 108th BYTE in a file?

    If so, add one Button to a Form and try this please:>>

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim fileBytes() As Byte = Nothing
        Dim fileName As String = ""
        Dim fileLength As Long
        Dim dlgResult As DialogResult
    
        Using OpenFileDialog1 As New OpenFileDialog
          OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    
          dlgResult = OpenFileDialog1.ShowDialog
          If dlgResult = Windows.Forms.DialogResult.OK And OpenFileDialog1.FileName <> String.Empty Then
    
            Using fs As New FileStream(OpenFileDialog1.FileName, FileMode.Open)
              Using rdr As New BinaryReader(fs)
                fileLength = My.Computer.FileSystem.GetFileInfo(OpenFileDialog1.FileName).Length
                fileBytes = rdr.ReadBytes(CInt(fileLength))
              End Using
            End Using
    
            'Byte 108 will be at array position 107 as arrays start at index number = 0
            fileBytes(107) = &H3F '<< this is HEX value 3F
            My.Computer.FileSystem.WriteAllBytes(fileName, fileBytes, False)
    
          End If
        End Using
    
      End Sub
    
    End Class


     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Or see
    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Sunday, August 14, 2011 5:52 PM