|
-
Jan 31st, 2008, 03:40 AM
#1
Thread Starter
New Member
Reading a file in Binary Form
Friends,
Please help me on "How to open a file(any type) and display its exact binary
structure in a text box. ie if we open an MP3 file the output is like
"110100010010111010100010......"
Thanks in Advance
-
Jan 31st, 2008, 04:18 AM
#2
Re: Reading a file in Binary Form
Interesting request. Hope you've got good eyes
Code:
Option Explicit
Private Sub Command1_Click()
Dim bytData() As Byte
Dim intI As Integer
Dim intJ As Integer
Dim intK As Integer
Dim intFile As Integer
Dim intByte As Integer
Dim lngSecs As Long
Dim strBin As String
Dim daThen As Date
Dim daNow As Date
daThen = Now
intFile = FreeFile
daThen = Now
Open "C:\readme1.txt" For Binary As intFile
ReDim bytData(LOF(intFile) - 1)
Get #intFile, , bytData
Close intFile
For intByte = 0 To UBound(bytData)
intI = bytData(intByte)
For intJ = 7 To 0 Step -1
intK = 2 ^ intJ
If (intI And intK) = intK Then
strBin = strBin & "1"
Else
strBin = strBin & "0"
End If
Next intJ
Text1.Text = Text1.Text & strBin
If intByte Mod 100 = 0 Then DoEvents
strBin = ""
Next intByte
daNow = Now
lngSecs = DateDiff("s", daThen, daNow)
MsgBox "Completed in " & lngSecs & " Seconds."
End Sub
Just substitute your file path and file for "C:\readme1.txt" in the above and run it. Text1 should be a multi-line TextBox with scroll bars.
A word of caution, a TextBox has a limit of about 64K characters. so the largest file you could display in this way would be about 8Kb. MP3 files tend to be a bit bigger than that. Also, it's going to take some time to process. I tested it on a file of about 3Kb and it took nearly 2 minutes to run.
What exactly are you trying to do ?
Last edited by Doogle; Jan 31st, 2008 at 04:36 AM.
Reason: Added the DoEvents bit so the UI is updated
-
Jan 31st, 2008, 08:45 AM
#3
Re: Reading a file in Binary Form
Looks like he wants to display the contents of a file much in the same way as a hex editor would display the contents of a file but he want it down to the actual binary format.
-
Feb 7th, 2008, 03:26 AM
#4
Thread Starter
New Member
Re: Reading a file in Binary Form
Thank U very much Doogle. Actually I wanna do an application to send file contents to parallel port, I done it with your code. Thank u sir.
-
Feb 7th, 2008, 03:46 AM
#5
Re: Reading a file in Binary Form
I've got a nasty feeling that you may have asked the wrong question
If you're trying to transfer a file from the computer to the Parallel Port I suspect you need to send Binary 1s and 0s not Character 1s and 0s.
A byte containing a Binary 1 = 0000 0001
A byte containing a Character 1 = 0100 0001 (ie the ASCI value for 1)
The code I gave effectively converts each binary 1 and 0 in each byte to it's character representation so they could be displayed in a TextBox.
A byte containing a Binary 1 = 0000 0001
A byte containing a Character 1 = 0100 0001 (ie the ASCI value for 1)
So, if you were to take the output of the code I gave, you'd be sending character 1s and 0s to the target device and unless that's going to re-assemble sets of 8 characters back into Binary values it's not going to understand at all.
Perhaps if you detailed exactly what you're trying to accomplish we'd be in a better sitation to help.
EDIT: Mind you if it's working as you expected then everything is OK
Last edited by Doogle; Feb 7th, 2008 at 04:37 AM.
-
Feb 8th, 2008, 04:17 AM
#6
Thread Starter
New Member
Re: Reading a file in Binary Form
Sir,
Actually I do the same you said.
With your code I send the binary values at the time of they were created (by converting each byte into binary) ie. in the if statements.
Is there is anything wrong sir, If I proceed with it.
ie, as on the code
the 1st byte code is 5 then the binary will be 00000101, right. then I send these 0s and 1s in that order. anything wrong sir?
Thankfully
Sandeep Thomas
-
Feb 8th, 2008, 04:39 AM
#7
Re: Reading a file in Binary Form
Ok. The difference is that the code I produced will take a Binary value of 5 - 0000 0101 and produce eight characters each representing the Asc value of 1 or 0.
ie For the binary value above, the code will generate:
Code:
Asc("0") Asc("0") Asc("0") Asc("0") Asc("0") Asc("1") Asc("0")
0011 0000 0011 0000 0011 0000 0011 0000 0011 0000 0011 0001 0011 0000
Asc("1")
0011 0001
Which is not what I think you want.
-
Feb 8th, 2008, 04:54 AM
#8
Re: Reading a file in Binary Form
Sorry, I clicked 'Post Quick Reply' too early.
As I said earlier, to transfer a file you need to send the Binary values as they are.
Code:
Option Explicit
Private Sub Command1_Click()
Dim bytData() As Byte
Dim intI As Integer
Dim intFile As Integer
intFile = FreeFile
Open "C:\readme1.txt" For Binary As intFile
ReDim bytData(LOF(intFile) - 1)
'
' Read the entire file into a Byte Array
'
Get #intFile, , bytData
Close intFile
'
' Now send it to the Parallel Port
'
For intI = LBound(bytData) To UBound(bytData)
Call SendToParallelPort(bytData(intI))
Next intI
End Sub
Where 'SendToParallelPort' is the routine you use to send data to the Parallel port. The above assumes that one byte is sent at a time. Perhaps if you post the actual code you are using it would help.
-
Feb 9th, 2008, 11:33 AM
#9
Thread Starter
New Member
Re: Reading a file in Binary Form
Hello sir,
firstly thank u very much sir 4 spending your valuable time 4 me. I changed my program as you said, that is now my program sends the byte data to the parallel port. I've follow a wrong idea of sending the 1s and 0s to the parallel port and I never think of it converts them based on their ascii.
-
Feb 9th, 2008, 11:41 AM
#10
Thread Starter
New Member
Re: Reading a file in Binary Form
Last edited by sandeepchirayil; Oct 20th, 2009 at 11:13 AM.
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
|