Solved

Open files in binary format

Posted on 2004-08-27
3
230 Views
Last Modified: 2010-05-02
Does anyone have the code to open any file in binary format and code to open a file which has been converted to binary back to its original state

e.g. opening a text file in binary so it comes out as 0's and 1's then change that back to whatever was in the text file...

Thanx for your help
0
Comment
Question by:mad_psyso
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11914474
I am not sure If I understood you properly, but here is the code to load file to byte array

Private Sub ReadBinary(FileName As String)
Dim FileName As String
Dim FN As Long
Dim arrData() As Byte
   
    FN = FreeFile
   
    Open FileName For Binary Access Read As #FN
   
    ReDim arrData(LOF(FN) - 1)

    Get #FN, , arrData()
   
    Close FN

End Sub
0
 

Author Comment

by:mad_psyso
ID: 11914609
Open a file in binary format and display it in a textbox then in another textbox convert it from binary back to its original format

If i has a textfile which contained "HELLO" textbox1 should display
01001000 01000101 01001100 01001100 01001111
which is the binary format

Then in the next textbox it should convert
01001000 01000101 01001100 01001100 01001111
back to its real state which would be back to "HELLO"
0
 
LVL 8

Accepted Solution

by:
mladenovicz earned 200 total points
ID: 11914728
Public Function BinaryToDecimal(Binary As String) As Long
    Dim n As Long
    Dim s As Integer
   
    For s = 1 To Len(Binary)
        n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ (s - 1)))
    Next s
   
    BinaryToDecimal = n
End Function

Public Function DecimalToBinary(DecimalNum As Long) As String
    Dim tmp As String
    Dim n As Long
   
    n = DecimalNum
   
    tmp = Trim(Str(n Mod 2))
    n = n \ 2
   
    Do While n <> 0
        tmp = Trim(Str(n Mod 2)) & tmp
        n = n \ 2
    Loop
   
    DecimalToBinary = tmp
End Function

Public Function StringToBinary(s As String) As String
Dim i As Integer
Dim R As String
    For i = 1 To Len(s)
        R = R & DecimalToBinary(Asc(Mid(s, i, 1))) & " "
    Next
    StringToBinary = R
End Function

'-- input string should be separated by spaces
Public Function BinaryToString(B As String) As String
Dim i As Integer
Dim Arr() As String
Dim R As String
   
    Arr = Split(B, " ")
   
    For i = LBound(Arr) To UBound(Arr)
        R = R & Chr(BinaryToDecimal(Arr(i)))
    Next
    BinaryToString = R
End Function
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question