Results 1 to 4 of 4

Thread: Binary Files

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Binary Files

    How can I create a binary file to look like the attached if opened in Notepad (combination of text and characters)?

    I've used the below code but when I open the file in Notepad it displays the word 'TEST'.

    Any suggestions?

    vb Code:
    1. Dim b() As Byte
    2. Open App.Path & "\test.abc" For Binary As #1
    3. s = "TEST"
    4.  
    5.     ReDim b(5) As Byte
    6.    
    7.     For i = 1 To Len(s)
    8.     'Now get details
    9.    
    10.     b(i - 1) = Asc(Mid$(s, i, 1))
    11.     Next
    12.        
    13.    
    14.     Put #1, , b()
    15.     Close #1
    Attached Images Attached Images  

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Binary Files

    Actually you wrote the string "TEST " into the file so that is what you get.

    Try this:
    Code:
    Sub Test()
        Dim i As Long
        Open "C:\!Tools\test.abc" For Binary As #1
            For i = 1 To 200
                Put #1, , CByte(Int(Rnd * 255 + 1))
            Next
        Close #1
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Binary Files

    You don't make binary files to get garbage in Notepad. The reason for binary files is that you can use datatypes such as Long, Integer, Byte and others for a specific kinds of uses. Sometimes data isn't even in an easy format, instead you have to read a bit of the file according to a specification just to know how to continue reading the file correctly (for example, ID3v2 tags in MP3 files, and the MP3 files themselves - ID3v2 just has readable text in the tag and not just binary data).

    A simple file could be:
    • 1000 32-bit values (Long)
    • These values contain a byte position to a starting point of a string.
    • After the 1000 values (which take 4000 bytes) the string data begins.
    • Each string ends with a character 0 (nullchar).


    That would be a pretty minimal file format. Something similar was used in the first Command & Conquer series of games.

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

    Re: Binary Files

    And if you want to do this for the purpose of the data not being legible, then I suggest encryption

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