Thread: Общие вопросы (General Questions)/Base64 encode decode VBS function

Base64 encode decode VBS function

www.motobit.com/tips/detpg_Base64Encode/


 


Function Base64Encode(inData)

  'rfc1521
  '2001 Antonin Foller, Motobit Software, http://Motobit.cz
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim cOut, sOut, I
  
  'For each group of 3 bytes
  For I = 1 To Len(inData) Step 3
    Dim nGroup, pOut, sGroup
    
    'Create one long from this 3 bytes.
    nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
      &H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))
    
    'Oct splits the long To 8 groups with 3 bits
    nGroup = Oct(nGroup)
    
    'Add leading zeros
    nGroup = String(8 - Len(nGroup), "0") & nGroup
    
    'Convert To base64
    pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)
    
    'Add the part To OutPut string
    sOut = sOut + pOut
    
    'Add a new line For Each 76 chars In dest (76*3/4 = 57)
    'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
  Next
  Select Case Len(inData) Mod 3
    Case 1: '8 bit final
      sOut = Left(sOut, Len(sOut) - 2) + "=="
    Case 2: '16 bit final
      sOut = Left(sOut, Len(sOut) - 1) + "="
  End Select
  Base64Encode = sOut
End Function

Function MyASC(OneChar)
  If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function




Re: Base64 encode decode VBS function

www.nonhostile.com/howto-encode-decode-base64-vb6.asp





Re: Base64 encode decode VBS function



    
        
            
            
        
    

            

In VB6, add a project reference to Microsoft XML, v2.6 (or later)


            

 


            

The following fuctions wrapper the encoding/decoding functionality:


            

            

Private Function EncodeBase64(ByRef arrData() As Byte) As String


            

 


            

    Dim objXML As MSXML2.DOMDocument

                Dim objNode As MSXML2.IXMLDOMElement

               

               
' help from MSXML

            
    Set objXML = New MSXML2.DOMDocument

               

                ' byte array to base64

                Set objNode = objXML.createElement("b64")

                objNode.dataType = "bin.base64"

                objNode.nodeTypedValue = arrData

                EncodeBase64 = objNode.Text


            

 


            

    ' thanks, bye

            
    Set objNode =
Nothing

                Set
objXML = Nothing


            

 


            

End Function


            

 


            

Private Function DecodeBase64(ByVal strData As String) As Byte()


            

 


            

    Dim objXML As MSXML2.DOMDocument

                Dim objNode As MSXML2.IXMLDOMElement

               

               
' help from MSXML

               
Set objXML = New MSXML2.DOMDocument

                Set objNode = objXML.createElement("b64")

                objNode.dataType = "bin.base64"

                objNode.Text = strData

                DecodeBase64 = objNode.nodeTypedValue

               

                ' thanks, bye

                Set objNode =
Nothing

                Set
objXML = Nothing


            

 


            

End Function


            


To test the above functions, the following code decodes encoded data, its output should match the input. The StrConv function is used to convert strings into byte arrays and vice versa.



Public Sub Main()


 


    Dim strData As String


 


    strData = EncodeBase64(StrConv("Greetings and Salutations", vbFromUnicode))

    Debug.Print strData 

    Debug.Print
StrConv(DecodeBase64(strData), vbUnicode)


 


End Sub


 


Output...


R3JlZXRpbmdzIGFuZCBTYWx1dGF0aW9ucw==


Greetings and Salutations


-- 25/02/2010 20:31:07: post edited by sergey.





Re: Base64 encode decode VBS function

C#-Base64 Encoding and Decoding



 



public static string base64Encode(string data)string encodedData = "";try

{


 


encData_byte = System.Text.


encodedData =


}


 


byte[] encData_byte = new byte[data.Length];Encoding.UTF8.GetBytes(data);Convert.ToBase64String(encData_byte);catch

{


 


// do it quietly

}


 


}


 


{


 


 


return encodedData;public static string base64Decode(string data)string result = "";try

{


System.Text.


System.Text.


 


 


 


utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);


result =


}


 


{


 


UTF8Encoding encoder = new System.Text.UTF8Encoding();Decoder utf8Decode = encoder.GetDecoder();byte[] todecode_byte = Convert.FromBase64String(data);int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);char[] decoded_char = new char[charCount];new String(decoded_char);catch // do it quietly

}


 


}


return result;


{