VB - Fast Base64 Encoding and Decoding-VBForums
Results 1 to 14 of 14

Thread: VB - Fast Base64 Encoding and Decoding

  1. #1

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    VB - Fast Base64 Encoding and Decoding

    Here's some code for encoding and decoding MIME Base64 (and fast). On my machine it'll do around 2MB a second encoding and 1MB a second decoding. The zip file contains both a class version (a little slower because of the overhead) and a module version. Enjoy! (yEnc encoder/decode to follow shortly).

    EDIT: Orig. zip removed, use the one below.
    Last edited by Comintern; Jul 10th, 2006 at 10:08 AM.

  2. #2
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: VB - Fast Base64 Encoding and Decoding

    nice code, but....

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim lCnt As Long
    3.    
    4.     For lCnt = 1 To 255
    5.         IntegrityTest lCnt
    6.     Next lCnt
    7.    
    8. End Sub
    9.  
    10. Private Sub IntegrityTest(lLen As Long)
    11.     Dim lCnt1 As Long, lCnt2 As Long
    12.     Dim sOriginal As String, sEncoded As String, sDecoded As String
    13.     Dim bytArr() As Byte
    14.    
    15.     ReDim bytArr(lLen) As Byte
    16.    
    17.     'create a binary string for testing
    18.    
    19.     Do
    20.         bytArr(lCnt1) = lCnt2
    21.         lCnt1 = lCnt1 + 1
    22.         lCnt2 = lCnt2 + 1
    23.         If lCnt2 = 256 Then lCnt2 = 0
    24.     Loop Until lCnt1 > UBound(bytArr)
    25.        
    26.     sOriginal = StrConv(bytArr, vbUnicode)
    27.    
    28.    
    29.     sEncoded = Encode64(sOriginal)
    30.    
    31.     sDecoded = Decode64(sEncoded)
    32.    
    33.     If sOriginal = sDecoded Then
    34.         Debug.Print "Data integrity test passed!"; lLen
    35.     Else
    36.         Debug.Print "Data integrity test failed!"; lLen
    37.     End If
    38.    
    39. End Sub

    well, try that code and see yourself

  3. #3

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: VB - Fast Base64 Encoding and Decoding

    Your test for equity will always fail if the length of the input string isn't divisible by 3. Reason being, the string is passed to the encoder ByRef and is padded with nulls so that it's length is divisible by 3. That means that the string that is actually encoded correctly, but when you look for equity later in the test code you aren't comparing against what was actually encoded. You can verify this by explicitly passing the strings to the encoding functions ByVal or passing it a copy of the string.

    I would like to thank you for making me take a look at this again--there was an error that my testing didn't catch dealing with how the end padding key is placed when the encoded data ends with a CrLf. That just needed a little one-line fix.

    Here's my test code, 10000 random length strings between 1 and 1000 characters.

    VB Code:
    1. Private Sub IntegrityBas()
    2.  
    3.     Dim sOrig As String, sTest As String, sEnc As String, sDec As String, lRep As Long
    4.    
    5.     Randomize
    6.    
    7.     For lRep = 1 To 10000
    8.         sOrig = TestString(Int(1000 * Rnd + 1))
    9.         sTest = sOrig
    10.         sEnc = Encode64(sTest)
    11.         sDec = Decode64(sEnc)
    12.         If sDec <> sOrig Then
    13.             Debug.Print "Data integrity test failed!"
    14.             Exit Sub
    15.         End If
    16.     Next lRep
    17.    
    18.     Debug.Print "Data integrity test passed!"
    19.    
    20. End Sub
    21.  
    22. Private Sub IntegrityCls()
    23.  
    24.     Dim sOrig As String, sTest As String, sEnc As String, sDec As String, lRep As Long, oEnc As Base64
    25.    
    26.     Randomize
    27.     Set oEnc = New Base64
    28.    
    29.     For lRep = 1 To 10000
    30.         sOrig = TestString(Int(1000 * Rnd + 1))
    31.         sTest = sOrig
    32.         sEnc = oEnc.Encode(sTest)
    33.         sDec = oEnc.Decode(sEnc)
    34.         If sDec <> sOrig Then
    35.             Debug.Print "Data integrity test failed!"
    36.             Set oEnc = Nothing
    37.             Exit Sub
    38.         End If
    39.     Next lRep
    40.    
    41.     Set oEnc = Nothing
    42.     Debug.Print "Data integrity test passed!"
    43.    
    44. End Sub
    45.  
    46. Private Function TestString(lLen As Long) As String
    47.  
    48.     Dim bTemp() As Byte, lPos As Long
    49.    
    50.     ReDim bTemp(lLen)
    51.     For lPos = LBound(bTemp) To UBound(bTemp)
    52.         bTemp(lPos) = Int((255 + 1) * Rnd)
    53.     Next lPos
    54.  
    55.     TestString = StrConv(bTemp, vbUnicode)
    56.  
    57. End Function
    Attached Files Attached Files

  4. #4
    New Member
    Join Date
    Aug 2006
    Posts
    1

    Re: VB - Fast Base64 Encoding and Decoding

    I tried the code using another language....Chinese to be precise. It doesn't work.

  5. #5

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: VB - Fast Base64 Encoding and Decoding

    What problem are you running into? This should encode any binary file, so the language of the file shouldn't be relevent. If it is used to encode unicode strings, you will need to compensate for the implicit unicode conversions that VB6 performs. Can you post a sample file for me to test with?

  6. #6
    New Member
    Join Date
    Apr 2007
    Posts
    3

    Re: VB - Fast Base64 Encoding and Decoding

    ComIntern! Your code looks good, but I am having trouble using it to encode data from a binary file in VB6. Have tried bunch of different ways to get the data out of the file and into a string to give to your encoder function, but the base64 comes back wrong. Could you post some VB6 example code as to how to do this?

    I have tried stuff like InputB from the file into a string, reading the file into a Byte (byte by byte) and concatenating them into a string, etc, but nothing seems to work.

    Thanks!

  7. #7
    New Member
    Join Date
    Apr 2007
    Posts
    3

    Re: VB - Fast Base64 Encoding and Decoding

    Here is an example of some code that doesn't seem to return the right base64:


    iFile = FreeFile()
    Open txtAttachmentFile.Text For Binary Access Read Lock Write As #iFile
    strAttachment = InputB(LOF(iFile), iFile)
    Close #iFile

    ' convert attachment to base64
    Set mybase64 = New Base64
    strAttachment = mybase64.Encode(strAttachment)

  8. #8

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: VB - Fast Base64 Encoding and Decoding

    You're reading the file as ANSI into a Unicode string. Use the Get function instead:
    Code:
        iFile = FreeFile()
        Open txtAttachmentFile.Text For Binary Access Read Lock Write As #iFile
        strAttachment = String$(LOF(iFile), Chr$(0))
        Get #iFile, , strAttachment
        Close #iFile

  9. #9
    New Member
    Join Date
    Apr 2007
    Posts
    3

    Re: VB - Fast Base64 Encoding and Decoding

    Thanks, O Great ComIntern. Works like a charm!

  10. #10
    New Member
    Join Date
    Apr 2007
    Posts
    1

    Smile Re: VB - Fast Base64 Encoding and Decoding

    Thank you very much for the code -- just what I was looking for!

    - Joe

  11. #11
    Junior Member
    Join Date
    Sep 2007
    Posts
    17

    Re: VB - Fast Base64 Encoding and Decoding

    This one help a lot in my project :

    Quote Originally Posted by Comintern
    You're reading the file as ANSI into a Unicode string. Use the Get function instead:
    Code:
        iFile = FreeFile()
        Open txtAttachmentFile.Text For Binary Access Read Lock Write As #iFile
        strAttachment = String$(LOF(iFile), Chr$(0))
        Get #iFile, , strAttachment
        Close #iFile
    But how to make it back to a single file when we want to decode it... I know that I need to convert it to binary first so that it can be converted into one single actual file. But I've got no idea in doing that plus I'm a beginner in VB6. Thanks....

  12. #12

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: VB - Fast Base64 Encoding and Decoding

    Quote Originally Posted by bearboss_85
    This one help a lot in my project :



    But how to make it back to a single file when we want to decode it... I know that I need to convert it to binary first so that it can be converted into one single actual file. But I've got no idea in doing that plus I'm a beginner in VB6. Thanks....
    Try:
    Code:
        iFile = FreeFile()
        Open file.bin For Binary As #iFile              'replace with the destination file name.
        Put #iFile, , sBinary
        Close #iFile
    file.bin would be replaced with the output filename, sBinary would be the decoded string.

  13. #13
    Junior Member
    Join Date
    Sep 2007
    Posts
    17

    Re: VB - Fast Base64 Encoding and Decoding

    I've got this originally from here :

    http://www.vbforums.com/showthread.php?t=514815

    It's my thread.. LaVolpe is helping a lot.. Since it's your code, I figure out that I might want to asked your opinion as well... Here's the code:

    Code:
    Private Sub cmdDecode_Click()
    Dim sBase64 As String
    Dim sJPG As String
    Dim iFile As Integer
    Dim myClass As Class1
    
        cdSave.Filter = "Joint Photo Group (*.jpg)|*.jpg|Joint Photo Expert Group (*.jpeg)|*.jpeg"
        cdSave.ShowSave
        If Err Then
            ' This code runs if the dialog was cancelled
            MsgBox "Dialog Cancelled"
        Else
            Set myClass = New Class1
            iFile = FreeFile()
            Open Text2.Text For Binary Access Read Lock Write As #iFile
            sJPG = String$(LOF(iFile), 0)
            Get #iFile, , sJPG
            Close #iFile
        
            sBase64 = myClass.Decode(sJPG)
            iFile = FreeFile()
            Open cdSave.FileName For Binary As #iFile
            Put #iFile, , sBase64
            Close #iFile
        End If
    
    End Sub
    I've got this kind of error :

    Run-time error '53'

    File not found


  14. #14
    Lively Member
    Join Date
    Oct 2007
    Posts
    126

    Re: VB - Fast Base64 Encoding and Decoding

    I am getting a "garbage at end of zipfile" message when trying to open a .zip file created with the decoded contents of a string using this decode code. Different .zip programs, like Enzip and ZipCentral, give slightly different versions of the same message.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.