Solved

Using strings with Binary files

Posted on 2003-10-24
7
316 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 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

11 Experts available now in Live!

Get 1:1 Help Now