|
-
Aug 2nd, 2001, 04:12 AM
#1
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.
-
Aug 2nd, 2001, 04:22 AM
#2
Retired VBF Adm1nistrator
To get an entire file into a variable ;
VB Code:
Option Explicit
Private Function GetFileContents(fileName As String) As String
If Dir(fileName) <> "" Then
Open fileName For Binary As #1
GetFileContents = Input(LOF(1), 1)
Close #1
End If
End Function
Private Sub Form_Load()
MsgBox GetFileContents("c:\autoexec.bat")
End Sub
Last edited by plenderj; Aug 2nd, 2001 at 04:39 AM.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 04:35 AM
#3
What about opening a new file and writing to it?
-
Aug 2nd, 2001, 04:38 AM
#4
Retired VBF Adm1nistrator
VB Code:
Open "c:\myFile.txt" For Binary As #1
Put #1, , "oiasjdosiajsod"
Close #1
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 04:47 AM
#5
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
-
Aug 2nd, 2001, 04:49 AM
#6
it dont even work for .txt files!!!!
-
Aug 2nd, 2001, 04:53 AM
#7
Retired VBF Adm1nistrator
First off, a post regarding looping thru the files.
When opening files for Input, one can read data as follows :
VB Code:
Dim strBuff As String
Open myFile For Input As #1
Do Until Eof(1)
Line Input #1, strBuff
Loop
Close #1
But you dont do it that way with binary files.
You do this instead :
VB Code:
Dim strBuff As String
Open myFile For Binary As #1
Do Until Loc(1) = Lof(1)
strBuff = Input(numberOfBytesToRead, 1)
'now do something with your numberOfBytesToRead Bytes of data
Loop
Close #1
Then regarding reading in some data and putting it somewhere else :
VB Code:
Private Sub Form_Load()
Open "c:\command.com" For Binary As #1
Open "c:\output.txt" For Binary As #2
Put #2, , Input(4096, 1)
Close #2
Close #1
End Sub
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 05:02 AM
#8
It creates the file, but it dont run
-
Aug 2nd, 2001, 05:16 AM
#9
Retired VBF Adm1nistrator
Well of course it wont run, you're only loading in 4096 bytes of command.com
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 05:18 AM
#10
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?
-
Aug 2nd, 2001, 05:33 AM
#11
Retired VBF Adm1nistrator
Well if you use
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]
-
Aug 2nd, 2001, 05:50 AM
#12
Originally posted by plenderj
Well if you use
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
-
Aug 2nd, 2001, 06:03 AM
#13
Retired VBF Adm1nistrator
VB Code:
Open "c:\command.com" For Binary As #1
MsgBox "Size of file : " & Lof(1)
Close #1
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 06:13 AM
#14
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.
-
Aug 2nd, 2001, 06:27 AM
#15
Retired VBF Adm1nistrator
Or you could juse use the filecopy statement.
VB Code:
Option Explicit
Sub bMoveFile(OldLoc As String, NewLoc As String)
Open OldLoc For Binary As #1
Open NewLoc For Output As #2
Print #2, Input(LOF(1), 1)
Close #2
Close #1
End Sub
Private Sub Form_Load()
bMoveFile "c:\windows\explorer.exe", "c:\myExplorer.exe"
End Sub
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 06:31 AM
#16
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)
-
Aug 2nd, 2001, 06:35 AM
#17
PowerPoster
Chenko, here another way to clone any filetype you like. Hope it help
VB Code:
Dim buff As String
Open "C:\Crsyb14.hlp" For Binary As #1
buff = String(LOF(1), Chr(0))
Get #1, 1, buff
Close #1
Open "C:\Crsyb15.hlp" For Binary As #1
Put #1, 1, buff
Close #1
regards
-
Aug 2nd, 2001, 06:38 AM
#18
Retired VBF Adm1nistrator
Oh yeah, forgot about that
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 06:43 AM
#19
-
Aug 2nd, 2001, 06:45 AM
#20
Retired VBF Adm1nistrator
Well my piece of code just above works !
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 07:21 AM
#21
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...?
-
Aug 2nd, 2001, 07:41 AM
#22
Retired VBF Adm1nistrator
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:
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]
-
Aug 2nd, 2001, 08:06 AM
#23
Yea, a lot of "dfweioan" & _ 
How do I Base64 Encode, I will probaly save each file away in a seperate module anyway
-
Aug 2nd, 2001, 08:11 AM
#24
Retired VBF Adm1nistrator
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]
-
Aug 2nd, 2001, 08:18 AM
#25
Ah ok, Looks like me gonna do some searchin' 
cheers anyways
-
Aug 2nd, 2001, 08:21 AM
#26
Retired VBF Adm1nistrator
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 2nd, 2001, 08:27 AM
#27
PowerPoster
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
-
Aug 2nd, 2001, 08:32 AM
#28
i used this... using the "DecodeFile" fucntion
VB Code:
Option Explicit
Private Type b64encoded
Byte1 As Byte
Byte2 As Byte
Byte3 As Byte
Byte4 As Byte
End Type
Private Type b64decoded
Byte1 As Byte
Byte2 As Byte
Byte3 As Byte
End Type
Private Type codecodeBytes
Byte1 As Byte
Byte2 As Byte
Byte3 As Byte
Byte4 As Byte
End Type
Dim keyByteA As codecodeBytes
Dim keyByteB As codecodeBytes
Dim keyByteC As codecodeBytes
Private Sub InitDecodeEncodeMachine()
'-------------------------------
keyByteA.Byte1 = &H3F
keyByteA.Byte2 = &H4
keyByteA.Byte3 = &H30
keyByteA.Byte4 = &H10
'-------------------------------
'-------------------------------
keyByteB.Byte1 = &HF
keyByteB.Byte2 = &H10
keyByteB.Byte3 = &H3C
keyByteB.Byte4 = &H4
'-------------------------------
'-------------------------------
keyByteC.Byte1 = &H3
keyByteC.Byte2 = &H40
keyByteC.Byte3 = &H3F
keyByteC.Byte4 = &H1
'-------------------------------
End Sub
'Decode source file encoded by base64 in
' to destination
Public Sub DecodeFile(ByVal srcFile As String, ByVal dstFile As String)
Dim tempBuffer As String * 78
Dim tempBufferNC As String * 74
Dim tempEncoded As b64encoded
Dim tempDecoded As b64decoded
Dim bResult As Byte
Dim iCntr As Long
Dim btResult As Byte
Call InitDecodeEncodeMachine
btResult = 0
iCntr = 0
Open srcFile For Random As #1 Len = 78
Open dstFile For Random As #2 Len = 1
Do While Not (EOF(1))
Get #1, , tempBuffer
iCntr = 0
Do While iCntr < Len(tempBuffer)
If Mid(tempBuffer, (iCntr + 1), 2) = vbCrLf Then Exit Do
tempEncoded.Byte1 = DeMapCode(Mid(tempBuffer, (iCntr + 1), 1))
tempEncoded.Byte2 = DeMapCode(Mid(tempBuffer, (iCntr + 2), 1))
tempEncoded.Byte3 = DeMapCode(Mid(tempBuffer, (iCntr + 3), 1))
tempEncoded.Byte4 = DeMapCode(Mid(tempBuffer, (iCntr + 4), 1))
bResult = 0
bResult = Base64Decode(tempEncoded, tempDecoded)
Select Case bResult
Case 1
Put #2, , tempDecoded.Byte1
Case 2
Put #2, , tempDecoded.Byte1
Put #2, , tempDecoded.Byte2
Case 3
Put #2, , tempDecoded.Byte1
Put #2, , tempDecoded.Byte2
Put #2, , tempDecoded.Byte3
End Select
'EOF encoded part
If (bResult = 0) Then Exit Do
'FOUR bytes as step
iCntr = iCntr + 4
Loop
'if end of encoded text
If (bResult = 0) Then Exit Do
Loop
Close #2
Close #1
End Sub
Private Function Base64Decode(srcBase64Encoded As b64encoded, dstBase64Decoded As b64decoded) As Byte
'return amoun of decoded bytes
If (srcBase64Encoded.Byte1 > 64) Then
Base64Decode = 0
Exit Function
End If
If ((srcBase64Encoded.Byte3 = 64) And (srcBase64Encoded.Byte4 = 64)) Then
dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _
(srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4
dstBase64Decoded.Byte2 = 0
dstBase64Decoded.Byte3 = 0
Base64Decode = 1
Exit Function
End If
If (srcBase64Encoded.Byte4 = 64) Then
dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _
(srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4
dstBase64Decoded.Byte2 = (srcBase64Encoded.Byte2 And keyByteB.Byte1) * keyByteB.Byte2 + _
(srcBase64Encoded.Byte3 And keyByteB.Byte3) / keyByteB.Byte4
dstBase64Decoded.Byte3 = 0
Base64Decode = 2
Exit Function
End If
dstBase64Decoded.Byte1 = (srcBase64Encoded.Byte1 And keyByteA.Byte1) * keyByteA.Byte2 + _
(srcBase64Encoded.Byte2 And keyByteA.Byte3) / keyByteA.Byte4
dstBase64Decoded.Byte2 = (srcBase64Encoded.Byte2 And keyByteB.Byte1) * keyByteB.Byte2 + _
(srcBase64Encoded.Byte3 And keyByteB.Byte3) / keyByteB.Byte4
dstBase64Decoded.Byte3 = (srcBase64Encoded.Byte3 And keyByteC.Byte1) * keyByteC.Byte2 + _
(srcBase64Encoded.Byte4 And keyByteC.Byte3) / keyByteC.Byte4
Base64Decode = 3
End Function
Private Function DeMapCode(srcChar As String) As Byte
If Len(srcChar) <> 1 Then
DeMapCode = 0
Exit Function
End If
Select Case srcChar
Case "A" To "Z"
DeMapCode = Asc(srcChar) - 65
Case "a" To "z"
DeMapCode = Asc(srcChar) - 97 + 26
Case "0" To "9"
DeMapCode = Asc(srcChar) - 48 + 52
Case "+"
DeMapCode = 62
Case "/"
DeMapCode = 63
Case "="
DeMapCode = 64
Case Else
DeMapCode = 65
End Select
End Function
...and it returned this in a file "1A" thats all!!
-
Aug 2nd, 2001, 09:02 AM
#29
Retired VBF Adm1nistrator
I would suggest trying a few different ones.
And then test by doing something like this :
VB Code:
Dim i As Long, j As Long
Dim myString As String
Dim worksOk As Boolean
Randomize
worksOk = True
For i = 0 To 1000
For j = 0 To 100
myString = myString & Rnd
Next j
worksOk = worksOk And (myString = Base64Decode(Base64Encode(myString)))
myString = ""
Next i
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]
-
Aug 2nd, 2001, 09:14 AM
#30
-
Aug 2nd, 2001, 09:35 AM
#31
Retired VBF Adm1nistrator
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]
-
Aug 2nd, 2001, 10:15 AM
#32
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.
-
Aug 2nd, 2001, 10:24 AM
#33
Fanatic Member
Jesus4u: The FSO is only good for ASCII text files, not binary.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|