Results 1 to 19 of 19

Thread: Reading and editing a binary file as a array of Ushorts?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Cool Reading and editing a binary file as a array of Ushorts?

    Hello dear programmers. I'm in a urgent need of help.
    I am developing a software that is in a nutshell a binary editor for eeprom and micro-processors in the world of Chip-Tuning (Cars), but it must view and edit the whole file as unsigned and signed shorts. I am out of ideas on how to make it. I have figured a way to read the file as UInt16 and copy the read contents into a textbox, but I can't figure out how to for example search the file for a array of ushorts and if it's found to replace it with a different array and save it to a file. Can abybody help me?

    Code:
    Dim bytes() As Byte
    bytes = File.ReadAllBytes("C:\MC68HC_dump.bin") ' location of the file that's going to be opened
    Dim Integers() As UShort = Enumerable.Range(0, bytes.Length \ 2).Select(Function(i) BitConverter.ToUInt16(bytes, i * 2)).ToArray
    TextBox1.Text = String.Join(" ", Integers)
    And by the way the files are from 512kb to 2mb so when loading the file the app freezes untill it's loaded... Any fix for that?

    So basically I need to read a bin file and search for : 25565, 23265, 00100, 00300 and if that's found replace it with 55555,55555,55555,55555 and save it with a savefiledialog.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Reading and editing a binary file as a array of Ushorts?

    You should be searching the array of numbers, not the string. There are specific algorithms for searching for a subset of items in a list so you should find information about one and make an effort to implement it in code. Once you find the index of the start of that subset, it's a simple matter of setting the appropriate number of elements from that index. I learned at least one of those algorithms in my Comp Sci days but it's been so long and I haven't had to use it specifically that I can't recall the name now.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Reading and editing a binary file as a array of Ushorts?

    To avoid freezing your app while performing cpu intensive operations, do your processing in a secondary thread. The easiest way to do that is to use a BackGroundWorker Component

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: Reading and editing a binary file as a array of Ushorts?

    Maybe a bit more specific on what should I search? I am a mid-level programmer I am not familiar with many things... I do know a way to search for BYTES and if found to return a address or offset of where it begins, but I do not know how to do it with Ushorts?

    Code:
    Function ByteMatch(ByVal Source As Byte(), ByVal Match As Byte()) As Integer
            For I As Integer = 0 To Source.Count - 1
                Dim Matched As Boolean = False
                If Match(0) = Source(I) Then
                    For J As Integer = 1 To Match.Count - 1
                        Matched = True
                        If Match(J) <> Source(I + J) Then
                            Matched = False
                            Exit For
                        End If
                        If Matched Then Return I
                        Exit For
                    Next J
                End If
            Next I
            Return -1
        End Function
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
              Dim G() As Byte = My.Computer.FileSystem.ReadAllBytes(fileName) ' file that will be searched for bytes
            Dim C() As Byte = {221, 0, 4, 0, 5} ' bytes for search
            Dim P As Integer = ByteMatch(G, C)
            If P = -1 Then MsgBox("No Match") Else MsgBox("Matched at " & P.ToString)
        End Sub
    Can we convert this code to somehow search for Ushorts?

    BY THE WAY: Does anybody know what the hell is this? In another software I can view bin files in 16bit-HiLo and 16-bit LoHi? I could not find any info on the internet about that.. For instance in HiLo: 18432 18432 but in LoHi: 00072 00072...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Reading and editing a binary file as a array of Ushorts?

    Quote Originally Posted by ProElectronicZR View Post
    I do know a way to search for BYTES and if found to return a address or offset of where it begins, but I do not know how to do it with Ushorts?
    Um, you change the type of the parameters from Byte to UShort. Why should the actual algorithm be any different? The steps are the same regardless of the type.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: Reading and editing a binary file as a array of Ushorts?

    How ever I tried it gets some error.. Can you show me how should the code look in order for it to work?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Reading and editing a binary file as a array of Ushorts?

    You already know what the code looks like. It looks exactly like what you have but with Byte changed to UShort. Instead of expecting us to write your code for you, how about you show us the code that you wrote and tell us what errors occur where? We can then point out what you did wrong so you can fix it. That's how it works.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Reading and editing a binary file as a array of Ushorts?

    Quote Originally Posted by ProElectronicZR View Post
    BY THE WAY: Does anybody know what the hell is this? In another software I can view bin files in 16bit-HiLo and 16-bit LoHi? I could not find any info on the internet about that.. For instance in HiLo: 18432 18432 but in LoHi: 00072 00072...
    Could you explain a bit more what you mean here?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: Reading and editing a binary file as a array of Ushorts?

    I do not know how to make a Array of Ushorts and how to read a file as ushort, when:
    Code:
     Dim G() As UShort = My.Computer.FileSystem.ReadAllBytes(fileName)
    I get a error, when I :
    Code:
       Dim C() As UShort = {25565, 23265, 100, 300} ' bytes for search
    I also get a error. I do not know how to read a file as ushort not how to make a ushort array or ushort list...
    Attached Images Attached Images  

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: Reading and editing a binary file as a array of Ushorts?

    Same file but in different view (one is in 16-bit HiLo other is in 16-bit LoHi) - they're terms used in the software I do not know what are the real names for them or how does VB.net define them. I get the LoHi variant when I read a file as Uint16 (Ushort) but I don't know how to read the file as the other one. (It seems that when I multiply the ushort * 256 i get that result but some numbers don't match)
    Attached Images Attached Images   

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: Reading and editing a binary file as a array of Ushorts?

    I figured out a way to read as uint16 but If i try to read it to the end of the file I get an error: : 'Unable to read beyond the end of the stream.'
    but when I set the end integer i.e: 10 then it will read the first 10 no problemo..

    Code:
     Using reader As New BinaryReader(File.Open(fileName, FileMode.Open))
                Dim pos As Integer = 0
                Dim length As Integer = reader.BaseStream.Length
                While pos < length
                    Dim value As Integer = reader.ReadUInt16()
                    pos += 1
                End While
            End Using

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Reading and editing a binary file as a array of Ushorts?

    Code:
    Dim length As Integer = reader.BaseStream.Length - 1

  13. #13
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Reading and editing a binary file as a array of Ushorts?

    You can read it as an array of UShorts like this:-
    Code:
        Private Function ReadFileAsUShort(ByVal fileName As String) As UShort()
            Dim lst As New List(Of UShort)
    
            Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read)
                Using br As New BinaryReader(fs)
                    Do
                        Try
                            lst.Add(br.ReadUInt16)
                        Catch ex As EndOfStreamException
                            Exit Do
                        End Try
    
                    Loop
    
                End Using
            End Using
    
    
            Return lst.ToArray
        End Function
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Reading and editing a binary file as a array of Ushorts?

    Quote Originally Posted by ProElectronicZR View Post
    I figured out a way to read as uint16 but If i try to read it to the end of the file I get an error: : 'Unable to read beyond the end of the stream.'
    but when I set the end integer i.e: 10 then it will read the first 10 no problemo..

    Code:
     Using reader As New BinaryReader(File.Open(fileName, FileMode.Open))
                Dim pos As Integer = 0
                Dim length As Integer = reader.BaseStream.Length
                While pos < length
                    Dim value As Integer = reader.ReadUInt16()
                    pos += 1
                End While
            End Using
    Since length is the number of bytes, and you're reading two bytes at a time, shouldn't that be "pos += 2".
    If the file is 100 bytes long, you should be doing 50 reads, not 100.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: Reading and editing a binary file as a array of Ushorts?

    Anybody know how do I change my code to search for ushort instead of bytes?

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Reading and editing a binary file as a array of Ushorts?

    Read your file as a List(of UShort), as shown by Niya (Post #13), Then use the List as Source, and the Match to find as an array of UShort...

    Code:
    Private Function Int16Match(ByVal Source As List(of UShort), ByVal Match As UShort()) As Integer
        For I As Integer = 0 To Source.Count - 1
            Dim Matched As Boolean = False
            If Match(0) = Source(I) Then
                For J As Integer = 1 To Match.Count - 1
                    Matched = True
                    If Match(J) <> Source(I + J) Then
                        Matched = False
                        Exit For
                    End If
                    If Matched Then Return I
                    Exit For
                Next J
            End If
        Next I
        Return -1
    End Function

  17. #17
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Reading and editing a binary file as a array of Ushorts?

    You could have also mapped an array of one type to an array of another type by using Buffer.Blockcopy when reading your file the way you originally tried.
    Array.Copy would have copied each single byte to a UShort, whereas Buffer.Blockcopy copies the raw array of bytes from the memory of one array to another array, so you get two bytes copied (mapped) to each UShort.
    You could remap an array of Int16 to UInt16 for instance, or UShort to UInt, or most any combination you want. You are just copying the specified number of bytes from one array of a given type to and array of another given type, starting at any point in the source array to any point in the destination array, based on byte offset and byte count, not array index offset and array index count.
    Code:
    Public Class Form1
    
      Dim G2() As UShort
      Dim fileName As String = "c:\c\LP73.dat"
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim G() As Byte = My.Computer.FileSystem.ReadAllBytes(fileName)
        ReDim G2((G.Length \ 2) - 1)
        Buffer.BlockCopy(G, 0, G2, 0, G.Length)  'copy bytes from byte array to Ushort array
      End Sub
    End Class
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: Reading and editing a binary file as a array of Ushorts?

    I still can't figure out how to define the list of ushorts.
    I tried like this but it doesn't find the result..

    Code:
    Dim fileName = ReadFileAsUShort(FILELOCATION)
    Dim Source As New List(Of UShort) From {8500,8500,8500,2500,2500}
    Dim P As Integer = Int16Match(Source, fileName)
    If P = -1 Then
      MessageBox.Show("NOTHING")
    Else
      MessageBox.Show(P.ToString)
    End If
    I can't believe that for days I can't figure out how to do this and no one could figure it out...
    Such a simple task and yet ...
    I just need to find 8500,8500,8500,2500,2500 and replace it with 10000 10000 10000 10000 10000... Simple as that... unbeliveble.. Thinking of quitting..

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Reading and editing a binary file as a array of Ushorts?

    Try this then...

    Code:
    Public Class Form1
    
      Dim G2() As UShort
      Dim fileName As String = "c:\c\LP73.dat"
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim G() As Byte = My.Computer.FileSystem.ReadAllBytes(fileName)
        ReDim G2((G.Length \ 2) - 1)
        Buffer.BlockCopy(G, 0, G2, 0, G.Length)  'copy bytes from byte array to Ushort array
    
        Dim arrayIndex As Integer = Int16Match(G2, New UShort() {8500,8500,8500,2500,2500}) 
    
        If arrayIndex > -1 Then
          For x As Integer = 0 to 4
            G2(arrayIndex + x) = 10000
          Next
        End If
    
        'G2 is your modified array of UShort
    
      End Sub
    
      Private Function Int16Match(ByVal Source As UShort(), ByVal Match As UShort()) As Integer
        For I As Integer = 0 To Source.Count - 1
            Dim Matched As Boolean = False
            If Match(0) = Source(I) Then
                For J As Integer = 1 To Match.Count - 1
                    Matched = True
                    If Match(J) <> Source(I + J) Then
                        Matched = False
                        Exit For
                    End If
                    If Matched Then Return I
                    Exit For
                Next J
            End If
        Next I
        Return -1
      End Function 
    
    End Class

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