Results 1 to 19 of 19

Thread: HexBox Control? View .bin file as ushort?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    HexBox Control? View .bin file as ushort?

    Is there any HexBox Control or type of way to view a long (2mb) .bin file as USHORT with some control that shows the addresses and everything, that allows editing and saving the modified file?
    File shown as example is WINOLS.
    Attached Images Attached Images  

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

    Re: HexBox Control? View .bin file as ushort?

    What would be the point of that? Looking at a byte stream as a series of UShorts is not the same thing a viewing it in hexadecimal. The hexadecimal view tells you what every single byte is. Looking at the stream as a series of UShorts really doesn't make that much sense. Like how should the bytes be interpreted? Little endian? Big endian? What happens to the left over byte if the byte stream's length is not a multiple of 2? This complicates things in my opinion.
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: HexBox Control? View .bin file as ushort?

    LittleEndian. Bytes stream is always multiple of 2 from every processor memory ever made.
    As you can see from the example, if i view the dump from the processor as 8bit nothing makes sense, it's just rubbish, but when I view it as Ushort(16bit) then I can see all the values as they should be.
    The files are from 256kb to 3mb max in size and I need to view them somehow, I found a method to read the file as ushort and copy it to a richtextbox or tb but it takes 7min to process a 1mb file...

    A link for the picture in better quality: https://www.mediafire.com/view/ux8h2dsjcvyg6qk
    Attached Images Attached Images  

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

    Re: HexBox Control? View .bin file as ushort?

    Oh so what you're actually saying is that your byte stream is simply an array of UShorts. Well this is a very specific format. Most likely you're going to have to roll your own editor for this.
    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

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

    Re: HexBox Control? View .bin file as ushort?

    Quote Originally Posted by ProElectronicZR View Post
    ...but it takes 7min to process a 1mb file...
    Add each line to a List of String. At the end set your TextBox Lines property to yourList.ToArray. It will definitely be quicker than 7 minutes

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: HexBox Control? View .bin file as ushort?

    Anybody got any ideas? Some links or advice?

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: HexBox Control? View .bin file as ushort?

    Can this code be converted so that it reads as ushort instead of byte?
    Code:
    'name = full location to file
    'rtb = richtextbox 
      Dim buff(1000000) As Byte
            Using fs = New FileStream(name, FileMode.Open)
                Using br = New BinaryReader(fs)
                    While True
                        Dim text = String.Empty
                        buff = br.ReadBytes(1000000)
                        Await Task.Run(Sub() text = String.Join(" ", buff.
                                    Select(Function(b) b.ToString("X2")))).
                                    ConfigureAwait(True)
                        rtb.AppendText(text)
                        If buff.Length < 1000000 Then
                            Exit While
                        End If
                    End While
                End Using
            End Using

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

    Re: HexBox Control? View .bin file as ushort?

    Like I said, you're probably going to have to design this Control yourself. To be clear, you want to be able to edit this array of UShorts using some kind of GUI, right?
    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: HexBox Control? View .bin file as ushort?

    If possible yes. But if i find a way to edit that souce that I've posted to read ushort I will figure everything out without a problem.. It works without a problem reads a 4mb file and appends text in 0.5 sec ... if it could read it as ushort it would be the solution to all my problems..

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

    Re: HexBox Control? View .bin file as ushort?

    Quote Originally Posted by ProElectronicZR View Post
    Can this code be converted so that it reads as ushort instead of byte?
    Code:
    'name = full location to file
    'rtb = richtextbox 
      Dim buff(1000000) As Byte
            Using fs = New FileStream(name, FileMode.Open)
                Using br = New BinaryReader(fs)
                    While True
                        Dim text = String.Empty
                        buff = br.ReadBytes(1000000)
                        Await Task.Run(Sub() text = String.Join(" ", buff.
                                    Select(Function(b) b.ToString("X2")))).
                                    ConfigureAwait(True)
                        rtb.AppendText(text)
                        If buff.Length < 1000000 Then
                            Exit While
                        End If
                    End While
                End Using
            End Using
    Something like this:-

    Code:
        Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'rtb = richtextbox 
            Dim name As String = "d:\ANSI-Unicode-2.png" 'Your file name goes here...
            Dim sb As New StringBuilder
    
            rtb.Text = "Processing file. Please wait....."
            rtb.ReadOnly = True
            Await Task.Run(Sub()
                               Using fs = New FileStream(name, FileMode.Open)
                                   Using br = New BinaryReader(fs)
                                       Do Until fs.Position = fs.Length
                                           sb.Append(br.ReadUInt16.ToString("X4"))
                                           sb.Append(" ")
                                       Loop
                                   End Using
                               End Using
                           End Sub)
    
            rtb.Text = sb.ToString
            rtb.ReadOnly = False
        End Sub
    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

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: HexBox Control? View .bin file as ushort?

    It seems that isn't ushort?
    This code gives me the values that I need:
    Code:
    Dim bin8() As Byte = System.IO.File.ReadAllBytes("C:\Users\PCX\Desktop\LONG_TEST.bin")
            Dim bin16(UBound(bin8) \ 2) As UInt16
            Try
                For i As UInteger = 0 To UBound(bin16)
                    bin16(i) = bin8(2 * i) + 256 * bin8(2 * i + 1)
                Next
            Catch ex As IndexOutOfRangeException
                bin16(UBound(bin16)) = bin8(UBound(bin8))
            End Try
    Attached Images Attached Images  
    Last edited by ProElectronicZR; Sep 16th, 2021 at 04:55 PM.

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

    Re: HexBox Control? View .bin file as ushort?

    UInt16 and UShort is the same thing.

    Niya's code was creating hex string representations of the 16-bit integers, which is what you were talking about, i.e. an editor representation.

    Your code is just getting the 16-bit integers values themselves, so two different requirements.
    If you just wanted to convert the file into UInt16 or UShort, we already mentioned how to do that in your other thread.

    The code given before would be
    Code:
            Dim bin8() As Byte = System.IO.File.ReadAllBytes("C:\Users\PCX\Desktop\LONG_TEST.bin")
            Dim bin16((bin8.ubound \ 2)) As UInt16
            Buffer.BlockCopy(bin8,0,bin16,0,bin8.Length)
    No need to loop, catch an exception or multiply and add pairs of bytes together, as the way you're multiplying and combining indicates the values are little endian, so just copy the bytes from the bin8 array to the bin16 array will be the correct byte order.

    You can try both way and compare the results to see that it works.

    Now you need to decide what you want to do with those values. Niya was showing the conversion from value to hex representation which you would need to present the number to the user to be edited.
    Your example editor shows the values in decimal, not hex, and perhaps that is what you want, in which case Niya's code isn't applicable, but your requirements of how you wanted the numbers presented wasn't clear. Perhaps you would like both as options.
    "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

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: HexBox Control? View .bin file as ushort?

    Can that code you wrote be adopted to @Niya's code? YES! It needs to show 16bit DECIMAL values. No I only need to see them in 16bit decimal, as you and others helped me with the code for replacing said values.

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

    Re: HexBox Control? View .bin file as ushort?

    If you want Niya's code to display decimal padded to 5 characters as the editor does, then change the one sb.append to pad the number with 0's and not convert to hex (no "X4")
    Code:
        sb.Append(br.ReadUInt16.ToString().PadLeft(5, "0"c))
    But note that the thread title does mention HexBox Control which implies hex, not decimal output.

    The way I read the file could be adapted to Niya's code, but since you need to loop through the values to write them to the textbox, it probably doesn't really save that much, as an example.

    But in the long run, I think if you really want to be able to make an editor, then you probably do want to read the file into a local array that can be maintained, modified and written back out. The other option is to have to scan the string from the textbox to convert the string back into an array of bytes to write to the file.
    Last edited by passel; Sep 16th, 2021 at 05:26 PM.
    "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
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: HexBox Control? View .bin file as ushort?

    Went ahead an merged my read, with Niya's string creation, as an example...
    Code:
        Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            'rtb = richtextbox 
            Dim bin8() As Byte = System.IO.File.ReadAllBytes("C:\Users\PCX\Desktop\LONG_TEST.bin")
            Dim bin16(UBound(bin8) \ 2) As UInt16
            Buffer.BlockCopy(bin8, 0, bin16, 0, bin8.Length)
    
            Dim sb As New StringBuilder
    
            rtb.Text = "Processing file. Please wait....."
            rtb.ReadOnly = True
            Await Task.Run(Sub()
                               For Each u16 As UInt16 In bin16
                                   sb.Append(u16.ToString().PadLeft(5, "0"c))
                                   sb.Append(" ")
                               Next
                           End Sub)
    
            rtb.Text = sb.ToString
            rtb.ReadOnly = False
        End Sub
    "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

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    26

    Re: HexBox Control? View .bin file as ushort?

    Its working. Is there a way to byte swap the bytes before joining them into a ushort?

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

    Re: HexBox Control? View .bin file as ushort?

    I guess the question there is why?
    The code you provided in post #11 doesn't swap the bytes before joining them into a ushort. It may look like it does, but it doesn't.
    Is it possible the editor shows the bytes in big endian order and you want to replicate that?
    "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: HexBox Control? View .bin file as ushort?

    You can choose between 8bit decimal, 8bit hex, 16bit decimal, 16bit hex, 16bit(HiLo) and 16bit(LoHi). Some processor memories byte swap so the ushort needs to be byte swapped before-hand so that what I see is what it is. But in my case I only need the ushort variants swapped or not swapped.
    Attached Images Attached Images  

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

    Re: HexBox Control? View .bin file as ushort?

    Well, I guess check for an option indicating you want to swap, and just loop through the array and swap the bytes before the Buffer.BlockCopy.
    Code:
            If bigendian Then
                Dim tmp As Byte
                For i As Integer = 0 To UBound(bin8) - 1 Step 2
                    tmp = bin8(i)
                    bin8(i) = bin8(i + 1)
                    bin8(i + 1) = tmp
                Next
            End If
    "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

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