Results 1 to 6 of 6

Thread: Base 64

  1. #1
    chenko
    Guest

    Base 64

    Does anyone have a Base64 encoding function?

    cheers.

  2. #2
    Addicted Member
    Join Date
    Jun 2001
    Posts
    183
    VB Code:
    1. Const sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    2.     ' --------------------------------------
    3.     '     ---------------------------------------
    4.     function Base64decode(ByVal asContents)
    5.         Dim lsResult
    6.         Dim lnPosition
    7.         Dim lsGroup64, lsGroupBinary
    8.         Dim Char1, Char2, Char3, Char4
    9.         Dim Byte1, Byte2, Byte3
    10.         if Len(asContents) Mod 4 > 0 Then asContents = asContents & String(4 - (Len(asContents) Mod 4), " ")
    11.         lsResult = ""
    12.        
    13.         For lnPosition = 1 To Len(asContents) Step 4
    14.             lsGroupBinary = ""
    15.             lsGroup64 = Mid(asContents, lnPosition, 4)
    16.             Char1 = INSTR(sBASE_64_CHARACTERS, Mid(lsGroup64, 1, 1)) - 1
    17.             Char2 = INSTR(sBASE_64_CHARACTERS, Mid(lsGroup64, 2, 1)) - 1
    18.             Char3 = INSTR(sBASE_64_CHARACTERS, Mid(lsGroup64, 3, 1)) - 1
    19.             Char4 = INSTR(sBASE_64_CHARACTERS, Mid(lsGroup64, 4, 1)) - 1
    20.             Byte1 = Chr(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)
    21.             Byte2 = lsGroupBinary & Chr(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF)
    22.             Byte3 = Chr((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
    23.             lsGroupBinary = Byte1 & Byte2 & Byte3
    24.            
    25.             lsResult = lsResult + lsGroupBinary
    26.         Next
    27.         Base64decode = lsResult
    28.     End function
    29.     ' --------------------------------------
    30.     '     ---------------------------------------
    31.     function Base64encode(ByVal asContents)
    32.         Dim lnPosition
    33.         Dim lsResult
    34.         Dim Char1
    35.         Dim Char2
    36.         Dim Char3
    37.         Dim Char4
    38.         Dim Byte1
    39.         Dim Byte2
    40.         Dim Byte3
    41.         Dim SaveBits1
    42.         Dim SaveBits2
    43.         Dim lsGroupBinary
    44.         Dim lsGroup64
    45.        
    46.         if Len(asContents) Mod 3 > 0 Then asContents = asContents & String(3 - (Len(asContents) Mod 3), " ")
    47.         lsResult = ""
    48.        
    49.         For lnPosition = 1 To Len(asContents) Step 3
    50.             lsGroup64 = ""
    51.             lsGroupBinary = Mid(asContents, lnPosition, 3)
    52.    
    53.             Byte1 = Asc(Mid(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
    54.             Byte2 = Asc(Mid(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
    55.             Byte3 = Asc(Mid(lsGroupBinary, 3, 1))
    56.    
    57.             Char1 = Mid(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)
    58.             Char2 = Mid(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
    59.             Char3 = Mid(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
    60.             Char4 = Mid(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)
    61.             lsGroup64 = Char1 & Char2 & Char3 & Char4
    62.            
    63.             lsResult = lsResult + lsGroup64
    64.         Next
    65.        
    66.         Base64encode = lsResult
    67.     End function

    BTW - my WINNT is on D:, all your sig shows me is

  3. #3
    Addicted Member
    Join Date
    Jun 2001
    Posts
    183
    DAMMIT! That was ASP, I had a great function I found for encoding basic-authentication, and I didn't keep a copy after I posted the code here a few weeks ago.

    I can't find the code again, it was in Google somewhere.

  4. #4
    chenko
    Guest
    Will that work in VB thou?

  5. #5
    Addicted Member
    Join Date
    Jun 2001
    Posts
    183
    Yes it should work, but the variables are all variants.

    If I find my better version, I'll post again.

  6. #6
    chenko
    Guest
    it works fine as it is, thanks!!

Posting Permissions

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



Click Here to Expand Forum to Full Width