Solved

Need MIME or Base64 encoding VB implementation

Posted on 1999-01-07
10
566 Views
Last Modified: 2008-03-17
I'm looking for a VB implementation of base64 encoding of a string. So no dll's or other modules and no delphi or c code.

Comments only please. I'll award the points when the correct answer has been given.
0
Comment
Question by:Mirkwood
10 Comments
 
LVL 2

Expert Comment

by:peterwest
ID: 1454617
Hi there,

Try the following - i've used it in the past and it works fine for me.  I'll post a routine for decoding too if you need it.

THanks

Pete
--------------------


Private Declare Function CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ptrDest As Any, ptrSource As Any, ByVal lenCopy As Long) As Long

Private Sub USAsciiToBase64(bIN() As Byte, b64() As Byte)

   Const TriPadding As Byte = 61 ' =
   Const Base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
   Static Base64(0 To 63) As Byte, Initialized As Boolean
   Dim TriBuf(0 To 3) As Byte, b As Byte, TriBits&
   Dim Trips&, Xtras&, bInLen&, bInPtr&, b64Ptr&

   If Not Initialized Then
      Initialized = True
      For b = 0 To 25
         Base64(b) = b + 65
         Base64(b + 26) = b + 97
      Next
      For b = 52 To 61
         Base64(b) = b - 4
      Next
      Base64(62) = 43
      Base64(63) = 47
   End If
   bInLen = UBound(bIN) - LBound(bIN) + 1
   Trips = bInLen \ 3
   Xtras = bInLen Mod 3
   ReDim b64(0 To IIf(Xtras, Trips + 1, Trips) * 4 + 1) '+1 for vbCrLf
   For bInPtr = 0 To Trips * 3 - 1 Step 3
      TriBuf(2) = bIN(bInPtr)
      TriBuf(1) = bIN(bInPtr + 1)
      TriBuf(0) = bIN(bInPtr + 2)
      CopyMemory TriBits, TriBuf(0), 3
      b64(b64Ptr) = Base64(TriBits \ &H40000)
      b64(b64Ptr + 1) = Base64((TriBits \ &H1000&) And &H3F&)
      b64(b64Ptr + 2) = Base64((TriBits \ &H40&) And &H3F&)
      b64(b64Ptr + 3) = Base64(TriBits And &H3F&)
      b64Ptr = b64Ptr + 4
   Next
   If Xtras > 0 Then
      TriBuf(0) = 0 'TriPadding
      TriBuf(2) = bIN(bInPtr)
      If Xtras > 1 Then
         TriBuf(1) = bIN(bInPtr + 1)
      Else
         TriBuf(1) = 0 'TriPadding
      End If
      CopyMemory TriBits, TriBuf(0), 3
      b64(b64Ptr) = Base64(TriBits \ &H40000)
      b64(b64Ptr + 1) = Base64((TriBits \ &H1000&) And &H3F&)
      b64(b64Ptr + 2) = IIf(Xtras = 1, TriPadding, Base64((TriBits \ &H40&) And &H3F&))
      b64(b64Ptr + 3) = TriPadding 'IIf(Xtras > 1, TriPadding, Base64(TriBits And &H3F&))
      b64Ptr = b64Ptr + 4
   End If
   b64(b64Ptr) = 13 'vbCr
   b64(b64Ptr + 1) = 10 'vbLf

End Sub

0
 
LVL 13

Author Comment

by:Mirkwood
ID: 1454618
With some byte & string conversion everything seems to be working OK. Please answer the question PeterWest to receive your points.
0
 
LVL 2

Accepted Solution

by:
peterwest earned 50 total points
ID: 1454619
Thanks,

Pete
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:mark2150
ID: 1454620
Hey Mirkwood!

You can download an entire MIME decoder program from my website - all source included!

www.cyberchute.com/rvbus/madmark

M

0
 

Expert Comment

by:jknickelbein
ID: 1454621
PeterWest,
Could you please post that decoding code for me?  It would be really helpful.
0
 
LVL 2

Expert Comment

by:peterwest
ID: 1454622
jknickelbein,

I'll happily post the code you request but not under this question - i'd rather have it in a seperate question so other people browsing the PAQ's can find it.  Point's aren't an issue for me - i'm certainly not point hungry - so if you post a question, just assign it 5 points or something, and i'll happily post the code.

Thanks

Pete

0
 
LVL 4

Expert Comment

by:idcanada
ID: 1454623
peterwest,
Could you please notify me when you do post this code.

Thanks.
0
 
LVL 2

Expert Comment

by:peterwest
ID: 1454624
idcanada,

How do you want me to notify you???

Pete

0
 
LVL 4

Expert Comment

by:idcanada
ID: 1454625
I thought this was a good notification :)
0
 

Expert Comment

by:drjoeross
ID: 7550885
I would be very interested in seeing the code for the Mime decoder. I could not currently locate it on this site.
Thanks,
Joe
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
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…

821 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