Results 1 to 3 of 3

Thread: Convert a binary file to hex code

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2024
    Posts
    8

    Post Convert a binary file to hex code

    Basically, how do you convert a binary file to its hex code in VB.NET just like this (VB6) thread on this forum: https://www.vbforums.com/showthread....t=binary%20hex

    So what I need is:

    1. Read a binary file.

    2. Convert each byte of the file to its ASCII value. Note that since it is a binary file, there are going to be a lot of non-printable characters in there. So A becomes 65, backspace char becomes 8 etc.

    3. Convert this array of ASCII values to the array of their 2 digit HEX conversions so that 65 becomes 41 and 8 becomes 08 etc.

    4. Convert this array to a string.

    5. Convert this whole string back to the original binary file. (Another function for this, obviously)

    Right now I have:

    Code:
    Dim scr_filename As String = My.Computer.FileSystem.SpecialDirectories.Desktop + "\mypic.jpg"
    
    Dim fdata = My.Computer.FileSystem.ReadAllBytes(scr_filename)
    
                Dim res As String = "", fstr As String, num As Integer
                'res = System.Text.Encoding.ASCII.GetBytes(fdata)
    
                fstr = System.Text.Encoding.Default.GetString(fdata)
    
                For i As Integer = 0 To (fstr.Length - 1)
                    num = Asc(fstr.Substring(i, 1))
                    res += num.ToString + " "
                Next
                MsgBox(res)
    Running this unholy, wicked code for a 2 kb file generates the result within one second. For a 20 kb file, it takes around 4 seconds. For a 120 kb file ... well, I executed the code and went away from the computer for more than half an hour. When I returned, the messagebox had still not appeared and the form was stuck/disabled.

    How do I exorcise this code, or, perhaps sacrifice it at the alter of Elder VB.NET and get a whole fresh code to do my bidding?
    Last edited by Asheekay; Aug 26th, 2024 at 07:46 AM. Reason: Updated the code

Tags for this Thread

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