Using strings with Binary files

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



peteangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dang123Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
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
Anthony PerkinsCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

cool12399Commented:
Yes, it is possible.

Do it pretty much as you just described (open file as binary, do manipulation, & save string).
0
cool12399Commented:
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
Anthony PerkinsCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.