Results 1 to 33 of 33

Thread: Binary Files

  1. #1
    chenko
    Guest

    Binary Files

    Can anyone give me some examples of working with files in binary? I want to store a whole file to a varible.

    thanks.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    To get an entire file into a variable ;

    VB Code:
    1. Option Explicit
    2.  
    3. Private Function GetFileContents(fileName As String) As String
    4.     If Dir(fileName) <> "" Then
    5.         Open fileName For Binary As #1
    6.             GetFileContents = Input(LOF(1), 1)
    7.         Close #1
    8.     End If
    9. End Function
    10.  
    11. Private Sub Form_Load()
    12.     MsgBox GetFileContents("c:\autoexec.bat")
    13. End Sub
    Last edited by plenderj; Aug 2nd, 2001 at 04:39 AM.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    chenko
    Guest
    What about opening a new file and writing to it?

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. Open "c:\myFile.txt" For Binary As #1
    2.         Put #1, , "oiasjdosiajsod"
    3.     Close #1
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    chenko
    Guest
    OK cheers.

    Is it possible to open a .exe file, read its contents then write it somewhere else, As I have tried with that code and it dont work

  6. #6
    chenko
    Guest
    it dont even work for .txt files!!!!

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    First off, a post regarding looping thru the files.


    When opening files for Input, one can read data as follows :
    VB Code:
    1. Dim strBuff As String
    2. Open myFile For Input As #1
    3.     Do Until Eof(1)
    4.         Line Input #1, strBuff
    5.     Loop
    6. Close #1

    But you dont do it that way with binary files.
    You do this instead :

    VB Code:
    1. Dim strBuff As String
    2. Open myFile For Binary As #1
    3.     Do Until Loc(1) = Lof(1)
    4.         strBuff = Input(numberOfBytesToRead, 1)
    5.         'now do something with your numberOfBytesToRead Bytes of data
    6.     Loop
    7. Close #1


    Then regarding reading in some data and putting it somewhere else :

    VB Code:
    1. Private Sub Form_Load()
    2.     Open "c:\command.com" For Binary As #1
    3.         Open "c:\output.txt" For Binary As #2
    4.             Put #2, , Input(4096, 1)
    5.         Close #2
    6.     Close #1
    7. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8
    chenko
    Guest
    It creates the file, but it dont run

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well of course it wont run, you're only loading in 4096 bytes of command.com
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    chenko
    Guest
    Damn I forgot about that, but the output file is say its 40KB, same as the EXE.

    how do i get the size of the file?

  11. #11
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well if you use

    VB Code:
    1. Input(4096, 1)

    Then you're only reading in 4Kb.


    Anyway, to get the size of a file open in binary mode use Lof(n).
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  12. #12
    chenko
    Guest
    Originally posted by plenderj
    Well if you use

    VB Code:
    1. Input(4096, 1)

    Then you're only reading in 4Kb.


    Anyway, to get the size of a file open in binary mode use Lof(n).
    I know 4096 is 4Kb

    I need a little more help with the Lof(n) thingy, Im crap with binary

  13. #13
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. Open "c:\command.com" For Binary As #1
    2.     MsgBox "Size of file : " & Lof(1)
    3. Close #1
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  14. #14
    chenko
    Guest
    this is the code that im testing it on
    [/Highlight]
    Sub bMoveFile(OldLoc, NewLoc)
    Dim byteSize
    Open OldLoc For Binary As #1
    byteSize = LOF(1)
    Close #1

    Open OldLoc For Binary As #1
    Open NewLoc For Binary As #2
    Put #2, , Input(byteSize, 1)
    Close #2
    Close #1
    End Sub
    [Highlight=VB]

    it dont work, my EXEs wont run, and when i do a txt file i look and the new file has funny chars at the beginning.

  15. #15
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Or you could juse use the filecopy statement.

    VB Code:
    1. Option Explicit
    2.  
    3. Sub bMoveFile(OldLoc As String, NewLoc As String)
    4.     Open OldLoc For Binary As #1
    5.         Open NewLoc For Output As #2
    6.             Print #2, Input(LOF(1), 1)
    7.         Close #2
    8.     Close #1
    9. End Sub
    10.  
    11. Private Sub Form_Load()
    12.     bMoveFile "c:\windows\explorer.exe", "c:\myExplorer.exe"
    13. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  16. #16
    chenko
    Guest
    This isnt really for moving files, I want to save them into a varible, and maybe write them somewhere if they are needed.

    I tried that code with the LOF(1) in the statment itself first, and that didnt work either

    does that code work for you? (on a .exe)

  17. #17
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Chenko, here another way to clone any filetype you like. Hope it help

    VB Code:
    1. Dim buff As String
    2.  
    3.     Open "C:\Crsyb14.hlp" For Binary As #1
    4.         buff = String(LOF(1), Chr(0))
    5.         Get #1, 1, buff
    6.     Close #1
    7.    
    8.     Open "C:\Crsyb15.hlp" For Binary As #1
    9.         Put #1, 1, buff
    10.     Close #1

    regards

  18. #18
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Oh yeah, forgot about that
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  19. #19
    chenko
    Guest
    Originally posted by plenderj
    Oh yeah, forgot about that
    ...and he gets it in one, LOL

    thanks both

  20. #20
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well my piece of code just above works !
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  21. #21
    chenko
    Guest
    well didnt work for me

    Also, is there a way I can store that varible in my project when I compile, like a const etc...?

  22. #22
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well worked for me

    Anyway if you wanted to store it as a const, you could just take the string value that is the file, and then just put into the src :

    VB Code:
    1. Public Const appString As String = "...."



    However
    The binary data would contain control characters that would **** up the source code. So you would probably have to Base64 encode it before storing it in your src.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  23. #23
    chenko
    Guest
    Yea, a lot of "dfweioan" & _


    How do I Base64 Encode, I will probaly save each file away in a seperate module anyway

  24. #24
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well the idea with Base64 encoding is that it removes all the funny characters from strings. You then Base54 decode the string into its original format.

    Its usually how attachments are put into emails.
    PlainText <> Binary

    Anyway there's lots of Base64 encoder functions on the net.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  25. #25
    chenko
    Guest
    Ah ok, Looks like me gonna do some searchin'

    cheers anyways

  26. #26
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  27. #27
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    I like the FileSytstemObject and the Textstream object

    Code:
    Sub OpenTextFileTest
        Const ForReading = 1, ForWriting = 2, ForAppending = 3
        Dim fs, f
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
        f.Write "Hello world!"
        f.Close
    End Sub

  28. #28
    chenko
    Guest
    i used this... using the "DecodeFile" fucntion

    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Private Type b64encoded
    5.     Byte1 As Byte
    6.     Byte2 As Byte
    7.     Byte3 As Byte
    8.     Byte4 As Byte
    9.     End Type
    10.  
    11.  
    12. Private Type b64decoded
    13.     Byte1 As Byte
    14.     Byte2 As Byte
    15.     Byte3 As Byte
    16.     End Type
    17.  
    18.  
    19. Private Type codecodeBytes
    20.     Byte1 As Byte
    21.     Byte2 As Byte
    22.     Byte3 As Byte
    23.     Byte4 As Byte
    24.     End Type
    25.     Dim keyByteA As codecodeBytes
    26.     Dim keyByteB As codecodeBytes
    27.     Dim keyByteC As codecodeBytes
    28.  
    29.  
    30. Private Sub InitDecodeEncodeMachine()
    31.    
    32.     '-------------------------------
    33.     keyByteA.Byte1 = &H3F
    34.     keyByteA.Byte2 = &H4
    35.     keyByteA.Byte3 = &H30
    36.     keyByteA.Byte4 = &H10
    37.     '-------------------------------
    38.     '-------------------------------
    39.     keyByteB.Byte1 = &HF
    40.     keyByteB.Byte2 = &H10
    41.     keyByteB.Byte3 = &H3C
    42.     keyByteB.Byte4 = &H4
    43.     '-------------------------------
    44.     '-------------------------------
    45.     keyByteC.Byte1 = &H3
    46.     keyByteC.Byte2 = &H40
    47.     keyByteC.Byte3 = &H3F
    48.     keyByteC.Byte4 = &H1
    49.     '-------------------------------
    50. End Sub
    51. 'Decode source file encoded by base64 in
    52. '     to destination
    53.  
    54.  
    55. Public Sub DecodeFile(ByVal srcFile As String, ByVal dstFile As String)
    56.     Dim tempBuffer As String * 78
    57.     Dim tempBufferNC As String * 74
    58.     Dim tempEncoded As b64encoded
    59.     Dim tempDecoded As b64decoded
    60.     Dim bResult As Byte
    61.     Dim iCntr As Long
    62.     Dim btResult As Byte
    63.     Call InitDecodeEncodeMachine
    64.     btResult = 0
    65.     iCntr = 0
    66.    
    67.     Open srcFile For Random As #1 Len = 78
    68.     Open dstFile For Random As #2 Len = 1
    69.    
    70.  
    71.  
    72.     Do While Not (EOF(1))
    73.         Get #1, , tempBuffer
    74.         iCntr = 0
    75.  
    76.  
    77.         Do While iCntr < Len(tempBuffer)
    78.             If Mid(tempBuffer, (iCntr + 1), 2) = vbCrLf Then Exit Do
    79.             tempEncoded.Byte1 = DeMapCode(Mid(tempBuffer, (iCntr + 1), 1))
    80.             tempEncoded.Byte2 = DeMapCode(Mid(tempBuffer, (iCntr + 2), 1))
    81.             tempEncoded.Byte3 = DeMapCode(Mid(tempBuffer, (iCntr + 3), 1))
    82.             tempEncoded.Byte4 = DeMapCode(Mid(tempBuffer, (iCntr + 4), 1))
    83.             bResult = 0
    84.             bResult = Base64Decode(tempEncoded, tempDecoded)
    85.  
    86.  
    87.             Select Case bResult
    88.                 Case 1
    89.                 Put #2, , tempDecoded.Byte1
    90.                 Case 2
    91.                 Put #2, , tempDecoded.Byte1
    92.                 Put #2, , tempDecoded.Byte2
    93.                 Case 3
    94.                 Put #2, , tempDecoded.Byte1
    95.                 Put #2, , tempDecoded.Byte2
    96.                 Put #2, , tempDecoded.Byte3
    97.             End Select
    98.        
    99.         'EOF encoded part
    100.         If (bResult = 0) Then Exit Do
    101.        
    102.         'FOUR bytes as step
    103.         iCntr = iCntr + 4
    104.     Loop
    105.     'if end of encoded text
    106.     If (bResult = 0) Then Exit Do
    107. Loop
    108.  
    109. Close #2
    110. Close #1
    111. End Sub
    112.  
    113.  
    114. Private Function Base64Decode(srcBase64Encoded As b64encoded, dstBase64Decoded As b64decoded) As Byte
    115.     'return amoun of decoded bytes
    116.  
    117.  
    118.     If (srcBase64Encoded.Byte1 > 64) Then
    119.         Base64Decode = 0
    120.         Exit Function
    121.     End If
    122.  
    123.  
    124.     If ((srcBase64Encoded.Byte3 = 64) And (srcBase64Encoded.Byte4 = 64)) Then
    125.         dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _
    126.         (srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4
    127.         dstBase64Decoded.Byte2 = 0
    128.         dstBase64Decoded.Byte3 = 0
    129.         Base64Decode = 1
    130.         Exit Function
    131.     End If
    132.  
    133.  
    134.     If (srcBase64Encoded.Byte4 = 64) Then
    135.         dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _
    136.         (srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4
    137.         dstBase64Decoded.Byte2 = (srcBase64Encoded.Byte2 And keyByteB.Byte1) * keyByteB.Byte2 + _
    138.         (srcBase64Encoded.Byte3 And keyByteB.Byte3) / keyByteB.Byte4
    139.         dstBase64Decoded.Byte3 = 0
    140.         Base64Decode = 2
    141.         Exit Function
    142.     End If
    143.     dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _
    144.     (srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4
    145.     dstBase64Decoded.Byte2 = (srcBase64Encoded.Byte2 And keyByteB.Byte1) * keyByteB.Byte2 + _
    146.     (srcBase64Encoded.Byte3 And keyByteB.Byte3) / keyByteB.Byte4
    147.     dstBase64Decoded.Byte3 = (srcBase64Encoded.Byte3 And keyByteC.Byte1) * keyByteC.Byte2 + _
    148.     (srcBase64Encoded.Byte4 And keyByteC.Byte3) / keyByteC.Byte4
    149.     Base64Decode = 3
    150.    
    151. End Function
    152.  
    153.  
    154. Private Function DeMapCode(srcChar As String) As Byte
    155.  
    156.  
    157.     If Len(srcChar) <> 1 Then
    158.         DeMapCode = 0
    159.         Exit Function
    160.     End If
    161.  
    162.  
    163.     Select Case srcChar
    164.         Case "A" To "Z"
    165.         DeMapCode = Asc(srcChar) - 65
    166.         Case "a" To "z"
    167.         DeMapCode = Asc(srcChar) - 97 + 26
    168.         Case "0" To "9"
    169.         DeMapCode = Asc(srcChar) - 48 + 52
    170.         Case "+"
    171.         DeMapCode = 62
    172.         Case "/"
    173.         DeMapCode = 63
    174.         Case "="
    175.         DeMapCode = 64
    176.         Case Else
    177.         DeMapCode = 65
    178.     End Select
    179. End Function


    ...and it returned this in a file "1A" thats all!!

  29. #29
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I would suggest trying a few different ones.
    And then test by doing something like this :

    VB Code:
    1. Dim i As Long, j As Long
    2. Dim myString As String
    3. Dim worksOk As Boolean
    4. Randomize
    5. worksOk = True
    6. For i = 0 To 1000
    7.     For j = 0 To 100
    8.         myString = myString & Rnd
    9.     Next j
    10.     worksOk = worksOk And (myString = Base64Decode(Base64Encode(myString)))
    11.     myString = ""
    12. Next i
    13. MsgBox "Test Successul : " & worksOk

    You'd probably have to change it around a little for different versions of the function.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  30. #30
    chenko
    Guest

  31. #31
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well assuming you've tested it and all is well, then you can start converting binary data into plaintext that you can copy and paste straight into your source code.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  32. #32
    chenko
    Guest
    The strings will be about 100,000 chars long!! LOL

    I made a program to split the lines up, and i made continuation on them, as you can only have 10 conncurrent... first try and it didnt work

    Can be arsed at the moment anymore.

  33. #33
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Jesus4u: The FSO is only good for ASCII text files, not binary.
    -Excalibur

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