[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using strings with Binary files

Posted on 2003-10-24
7
Medium Priority
?
337 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
6 Comments
 
LVL 9

Accepted Solution

by:
Dang123 earned 252 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 248 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

834 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