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
Printable View
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
Interesting request. Hope you've got good eyes
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.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
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 ?
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.
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.
I've got a nasty feeling that you may have asked the wrong question :confused:
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 :D
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
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:
Which is not what I think you want.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
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.
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.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
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.
THank u