locked
Convert file to its hex value and that hex value to its exe

    General discussion

  • Hii

    I have been converting a file into its hex value.But cannot convert it back into same exe value

    Let me get in detail

    Here is the code

    Imports System.IO

    Sub Main()

     

            Dim fileName As String = "ABC.exe"

     

            Dim data As Byte() = File.ReadAllBytes(fileName)

     

            Dim fs As FileStream = File.OpenWrite(fileName & ".txt")

            Dim sw As StreamWriter = New StreamWriter(fs)

     

            Dim pos As Integer = 0

            Dim line As Integer = 0

     

            While pos < data.Length

                sw.Write(String.Format("{0}:", line.ToString("0000")))

     

                For i As Integer = 1 To 20

                    sw.Write(String.Format(" {0:x2}", data(pos)))

                    pos += 1

                    If pos = data.Length Then

                        Exit For

                    End If

                Next

     

                sw.WriteLine()

                line += 1

            End While

     

            sw.Flush()

            sw.Close()

            fs.Close()

        End Sub

    Now,When i call the main method an text file called ABC.exe.txt is generated.

    First of all instead of ABC.exe.txt can we have ABC.txt only

    and secondly can how can i convert this hex values back to exe.. at least compile it in vb or any other language

    Thank you for your time

    Saturday, August 20, 2011 5:41 AM

All replies

  • Cant anyone help me????
    Saturday, August 20, 2011 9:32 AM
  • First of all instead of ABC.exe.txt can we have ABC.txt only 

            Dim inFileName As String = "ABC.exe"
            Dim outFileName As String = IO.Path.GetFileNameWithoutExtension(inFileName) & ".txt"

    Saturday, August 20, 2011 10:13 AM
  • and secondly can how can i convert this hex values back to exe.. at least compile it in vb or any other language


            Dim newfilename As String = IO.Path.GetFileNameWithoutExtension(inFileName) & ".new"
            fs = IO.File.OpenWrite(newfilename)
            
    For Each SLine As String In IO.File.ReadAllLines(outFileName)
                
    Dim S As String() = SLine.Split(" ")
                For I As Integer = 1 To S.Count - 1
                    fs.WriteByte(Convert.ToByte(S(I), 16))
                
    Next
            Next
            fs.Close()

    Saturday, August 20, 2011 10:43 AM
  • First of all thanks...After replacing the code i get an error "close is not a member of string" what do i do now?
    Saturday, August 20, 2011 2:32 PM
  • and more what about the second code ...where should i put it???
    Saturday, August 20, 2011 3:51 PM
  • The code example I provided does not include 'close'.  What line of code is creating this error?
    Saturday, August 20, 2011 10:00 PM
  • the second last line  "fs.close()"
    Sunday, August 21, 2011 5:27 AM
  • and also i am using vb 2008...if that would help
    Sunday, August 21, 2011 5:41 AM
  • That line did not create an error in the original code.  What changes have you made when you included the code to fix the problem with the incorrect file name?
    Sunday, August 21, 2011 6:01 AM
  • You need to include it in whatever routine you are using to convert the file back from text.   My code example assumed that it was simply done at the end of the routine you are currently using, but I guess it is more likely that you will include it in a different method, such as the click event for a different button.  It all depends on how you want the two conversions to work.
    Sunday, August 21, 2011 6:04 AM
  • i didnt get it....

    I declared a new sub with the above code

    then call that code in button click event

     

    Ok let me make it clear

    I want the file to be converted into its hex values and  that hex values should be stored in array

    and from that array convert it back to exe format

     

     

    Sunday, August 21, 2011 7:44 AM
  • I declared a new sub with the above code then call that code in button click event 

    That won't work unless you make some changes.  For instance, the code I posted assumes that fs has already been declared as a filestream, which it was when the code was in Sub Main.  If you are inserting that code into a new method then you need to declare fs properly, as per the original code.  Also, the original filename will not be available to convert to the new output file name with a different extension.

    Any code posted here needs to be adapted for the particular way you actually use it - you can't just cut and paste.

    Ok let me make it clear

    I want the file to be converted into its hex values and that hex values should be stored in array and from that array convert it back to exe format

    That's far from clear, unfortunately.  The original code converted the binary file to a text file, and you said that it needed to be converted back into the original binary file.  That's what the code I provided does.   There was no mention of an array.

    If you wanted to convert it to an array, you would not have started with the code you posted.  What sort of array should it be?  Should it be a string array?

    Sunday, August 21, 2011 8:00 AM
  • lets not call it an array .......it is a stack....i want to pt all those bytes in stack...!!
    Sunday, August 21, 2011 8:14 AM
  • What is a stack?  Why has your requirement changed from converting the file to and from a text file with that particular format?  It seems that you are now asking for asistance with a very different task that has little to do with the code you posted or the question you originally asked.  You should indicate that this thread is now complete, as the question that you asked has been answered, and you should ask a new question with the new requirement.  Be sure to describe exactly what you mean by 'stack'.

    Sunday, August 21, 2011 9:15 AM
  • well my goal is to 

    1.Take an file(exe,dll,etc)

    2.Convert it into hex

    3.place that hex values in a stack

    4.Execute the values inside the stack to its original form(i.e. take the elements out of stack and then convert it to a compile format)

    I hope that makes my point clear..and actually i am a newbie i just wrote this code to convert a file into its hex values thats it

    plz help

    Sunday, August 21, 2011 12:39 PM
  • The task you are now describing has nothing to do with your original question or the code you posted.  That question has now been answered.  You should mark this thread as answered and move on to this new task in a new thread.

    The use of 'hex' is wrong.  Hex is a format used to express binary data as a displayable string.  The data you are manipulating is binary, and it should not be converted to hex.

    There is no need to use a 'stack'.  The data in the EXE or DLL can be accessed sequentially (if that's what the stack is intended to do).  Your 'stack' is probably nothing more than an array of bytes.

    You will need to explain what you mean by 'convert it to compile format', as the EXE or DLL file is already compiled and is ready to execute - it doesn't need any conversion.

    Sunday, August 21, 2011 9:50 PM