Solved

Using strings with Binary files

Posted on 2003-10-24
7
312 Views
Last Modified: 2010-05-01
This may seem like a very simple question but...

I wish to open a file as Binary (images etc) and apply a simple compression routine to it.
I have a working compression routine that can handle strings.

I wish to open the file as binary and save the contents as a string in order to pass it through the compression function, is this possible?

Thanks



0
Comment
Question by:peteang
  • 3
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
Dang123 earned 63 total points
ID: 9617045
You can do base64 encoding to make binary data into workable strings, but this would expand the data (you would be representing the data with fewer possible "characters") below is a routine to load a binary file and do a base64 encode (I used it as part of an email module I have).



Private Sub EncodeAndSendMIME(ByVal strFile As String)
    '*****************************************************************************************
    '
    ' Parameters:       strFile - string containing full path and file name
    '
    ' Global Updates:   No global variables should be updated
    '
    ' Glabal Read:      m_bytBase64Byt - encoding characters
    '
    ' Return:           No return value - Sub
    '
    ' Description:
    '   Open the file to be attached and read in the characters.  Send the characters through
    '   the Base64 encoder before transmission through open socket.
    '
    '*****************************************************************************************
    ' For Encoding BASE64
    On Error GoTo ErrorCode
   
    Dim intEncodePointer    As Integer  ' Calculated index into array of encoding characters
    Dim bytBin(3)           As Byte     ' Array to hold data from file to convert
    Dim strScratch          As String   ' Scratch to hold encoded characters as they're built
    Dim lngFileLengthAdj    As Long     ' Length of file to encode, rounded to multiple of three
    Dim lngIndex            As Long     ' Loop index
    Dim lngFileIn           As Long     ' Input file handle
   
    Erase bytBin
    lngFileLengthAdj = 0: lngIndex = 0: lngFileIn = 0: intEncodePointer = 0:
    strScratch = ""
   
    ' Gets the next free file number
    lngFileIn = FreeFile
   
    ' Open Base64 input file
    Open strFile For Binary As lngFileIn
   
    Call SendData(strScratch & vbCrLf, m_blnShowAttach)
    strScratch = ""
   
    lngFileLengthAdj = LOF(lngFileIn) - (LOF(lngFileIn) Mod 3)
   
    For lngIndex = 1 To lngFileLengthAdj Step 3
       
        ' Read three bytes
        Get lngFileIn, , bytBin(0)
        Get lngFileIn, , bytBin(1)
        Get lngFileIn, , bytBin(2)
       
        ' Always wait until there're more then 64 characters
        If Len(strScratch) > 64 Then
           
            strScratch = strScratch & vbCrLf
            Call SendData(strScratch, m_blnShowAttach)
            strScratch = ""
           
        End If
       
        ' Calc Base64-encoded char
        intEncodePointer = (bytBin(0) \ 4) And &H3F ' Right shift 2 bits (&H3F=111111b)
        strScratch = strScratch & m_bytBase64Byt(intEncodePointer)  ' The string strScratch holds
                                                                    ' the encoded chars
       
        intEncodePointer = ((bytBin(0) And &H3) * 16) Or ((bytBin(1) \ 16) And &HF)
        strScratch = strScratch & m_bytBase64Byt(intEncodePointer)
       
        intEncodePointer = ((bytBin(1) And &HF) * 4) Or ((bytBin(2) \ 64) And &H3)
        strScratch = strScratch & m_bytBase64Byt(intEncodePointer)
       
        intEncodePointer = bytBin(2) And &H3F
        strScratch = strScratch & m_bytBase64Byt(intEncodePointer)
       
    Next lngIndex
   
    ' Now, you need to check if there is something left
    If Not (LOF(lngFileIn) Mod 3 = 0) Then
       
        ' Reads the number of bytes left
        For lngIndex = 1 To (LOF(lngFileIn) Mod 3)
            Get lngFileIn, , bytBin(lngIndex - 1)
        Next lngIndex
       
        ' If there are only 2 chars left
        If (LOF(lngFileIn) Mod 3) = 2 Then
            intEncodePointer = (bytBin(0) \ 4) And &H3F ' Right shift 2 bits (&H3F=111111b)
            strScratch = strScratch & m_bytBase64Byt(intEncodePointer)
           
            intEncodePointer = ((bytBin(0) And &H3) * 16) Or ((bytBin(1) \ 16) And &HF)
            strScratch = strScratch & m_bytBase64Byt(intEncodePointer)
           
            intEncodePointer = ((bytBin(1) And &HF) * 4) Or ((bytBin(2) \ 64) And &H3)
            strScratch = strScratch & m_bytBase64Byt(intEncodePointer)
           
            strScratch = strScratch & "="
           
        Else ' If there is only one char left
            intEncodePointer = (bytBin(0) \ 4) And &H3F ' Right shift 2 bits (&H3F=111111b)
            strScratch = strScratch & m_bytBase64Byt(intEncodePointer)
           
            intEncodePointer = ((bytBin(0) And &H3) * 16) Or ((bytBin(1) \ 16) And &HF)
            strScratch = strScratch & m_bytBase64Byt(intEncodePointer)
           
            strScratch = strScratch & "=="
        End If
    End If
   
    ' Send the characters left
    If strScratch <> "" Then
        strScratch = strScratch & vbCrLf
        Call SendData(strScratch, m_blnShowAttach)
    End If
   
    ' Send the last part of the MIME Body
    strScratch = ""
   
    Close lngFileIn
   
    Exit Sub
ErrorCode:
    RaiseEvent StatusUpdate(Err.Number, "SMTPMail - " & Err.Description, SMTPMail_SetError)
End Sub
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 62 total points
ID: 9617730
It seems like you already have the compression algorithm and all you need is to get the file contents into a string, if so the following will work:

Dim Handle As Integer
Dim Buffer As String

Handle = FreeFile
Open "your image file name goes here" For Binary As Handle
Buffer = Space$(LOF(Handle))
Get Handle, , Buffer
Close Handle

Anthony
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9617737
You can put the following in a function so that it returns the contents of the file in a string as follows:

Function GetFileContents(ByVal FileName As String) As String
Dim Handle As Integer
Dim Buffer As String

Handle = FreeFile
Open "your image file name goes here" For Binary As Handle
Buffer = Space$(LOF(Handle))
Get Handle, , Buffer
Close Handle
GetFileContents = Buffer

End Function

Anthony
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Expert Comment

by:cool12399
ID: 9617858
Yes, it is possible.

Do it pretty much as you just described (open file as binary, do manipulation, & save string).
0
 
LVL 10

Expert Comment

by:cool12399
ID: 9707419
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Split points between Dang123, acperkins, and Cool12399
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Cool12399
EE Cleanup Volunteer
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9727497
cool12399,

What exactly did you contribute to this question?  Also, this question is only 18 days, old.  Should you not be assigned questions that are years old, not days old.

Anthony



0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now