Results 1 to 9 of 9

Thread: Converting a binary file to hex>>file

  1. #1

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Converting a binary file to hex>>file

    This would be a project to read a binary file such dat files or exe files or even dlls but i found this extremely slow doing it. A 100kb file was taking ~1 minute, 1 mb file..well i gave up and closed the process.
    I would like to see this working faster if there is a way to do it..

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim arItems() As Byte
    5. Dim sText$
    6. Dim sTemp$
    7. Dim i As Double
    8.  
    9.     Open "tmp001.dat" For Binary As #1
    10.       ReDim arItems(LOF(1))
    11.       Get #1, , arItems
    12.     Close #1
    13.    
    14.     For i = LBound(arItems) To UBound(arItems) - 1
    15.         sText = sText & Hex(arItems(i))
    16.     Next i
    17.    
    18.     Open "hex_output.txt" For Append As #2
    19.         For i = 1 To Len(sText) Step 32
    20.             sTemp = Mid(sText, i, 32)
    21.             Print #2, sTemp
    22.         Next i
    23.     Close #2
    24.  
    25. End Sub
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Converting a binary file to hex>>file

    I never understood this fascination about HEX...

    I don't see any purpose whatsoever to convert a file to a HEX file...

    I would understand if you want to display the contents in HEX... but converting to another file in HEX, it will make the file size double !

    Anyways, the following should be better, but there is still room for improvement:
    VB Code:
    1. Dim arItems() As Byte
    2.     Dim K As Long, BPos As Long, SPos As Long, FileSize As Long
    3.     Dim OutData As String
    4.    
    5.     Open "c:\test.txt" For Binary Access Read As #1
    6.         FileSize = LOF(1)
    7.         ReDim arItems(FileSize - 1)
    8.         Get #1, , arItems
    9.     Close #1
    10.    
    11.     OutData = String(FileSize * 2 + (FileSize \ 16) * 2, "-")
    12.    
    13.     BPos = 0
    14.     SPos = 1
    15.    
    16.     Do Until BPos + 16 >= UBound(arItems)
    17.         For K = 1 To 16
    18.             Mid$(OutData, SPos, 2) = Right$("0" & Hex$(arItems(BPos)), 2)
    19.            
    20.             BPos = BPos + 1
    21.             SPos = SPos + 2
    22.         Next K
    23.        
    24.         Mid$(OutData, SPos, 2) = vbNewLine
    25.         SPos = SPos + 2
    26.     Loop
    27.    
    28.     For K = 0 To UBound(arItems) - BPos
    29.         Mid$(OutData, SPos, 2) = Right$("0" & Hex$(arItems(BPos)), 2)
    30.        
    31.         BPos = BPos + 1
    32.         SPos = SPos + 2
    33.     Next K
    34.    
    35.     Open "c:\hex_output.txt" For Binary Access Write As #2
    36.         Put #2, , OutData
    37.     Close #2

  3. #3

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Converting a binary file to hex>>file

    I can explain it better, my idea is to build some sort of program that will allow to write a backup program. This program will be written with default values that i plan to replace. All this heavy work is because i can't have with me all the time a notebook with vb installed to recompile the backup program with diferent values. Also i don't want that ppl change the paramethers or i would make the backup program to use a ini. Some times, ppl who don't know what they are doing, damage the program and then the backups aren't correctly donne. (I'v tryed the ini before and ppl changed/damaged the defenitions).
    Now i'm facing a bigger problem. Changing the hex values and writing the executable backup is easy, the hard part is how will i be able to include the huge hexed backup inside the main program...any idea?
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Converting a binary file to hex>>file

    So, in short what you want to do, is to save some settings, but you don't want the user to be able to modify the settings ?

  5. #5

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Converting a binary file to hex>>file

    exactly.
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Converting a binary file to hex>>file

    Quote Originally Posted by TDQWERTY
    exactly.
    Well, if you would have said that from the beginning, it would've been much easier...

    Did you consider Encryption ? (I'm the king of encryption )
    Or to make the file(s) hidden to the user (And ADS file...) ?

  7. #7

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Converting a binary file to hex>>file

    yes, i did, they deleted the ini file :\ i guess that didn't worked very well
    It's sad to say this but there are ppl that are totaly stupid and can't accept what we tell them, "Do not change anything inside this folder X" - well, they deleted the folder...

    It would be way simple if all i had to use was one file that they can't touch the configs...
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Converting a binary file to hex>>file

    Make an ADS file:
    VB - How to use Alternate Data Stream files

    Or put your settings in the Registry, and don't use names that are the same as the application name. That way it's more difficult for them to find the keys/values.
    Last edited by CVMichael; Mar 8th, 2006 at 04:59 PM.

  9. #9

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Converting a binary file to hex>>file

    Regestry is out of question, i don't want to use that, it gives me more work to change when i need. The Alternate Data Stream files are a possibility too...and i will probably use it. I didn't knew that method

    Edit:
    Two things that i didn't understood that you said:
    But that's not all... you can EXECUTE EXEs from ADS also !
    Check this thread:
    http://www.vbforums.com/showthread.php?t=380326
    Look at the attachent I have in post #20
    Could i also protect the program?
    An other question is, could i use a ini file using the ADS method too?
    I'm not sure if i understood 100% of what/how it does/it works..

    Edit2: Is that considered a virus?
    Last edited by TDQWERTY; Mar 8th, 2006 at 09:06 PM.
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

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