How to convert a base 64 string to byte array in C++?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abeda
    New Member
    • Sep 2010
    • 2

    How to convert a base 64 string to byte array in C++?

    I have a wstring that has a base 64 string. I need to extract a byte array from this base 64 string. How can I do this? I am implementing a COM Server in C++. Please help
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please clarify what you want to do. It would help if you provided an example showing the expected output for a particular input value.

    Comment

    • abeda
      New Member
      • Sep 2010
      • 2

      #3
      Basically, I have a .Net component (in C#) that would send the stream of bytes to my COM server after converting it into a base 64 string as follows:

      Code:
       //.Net code
        byte[] arr = new byte[2];
        arr[0] = 1;
        arr[1] = 2;
        string dataByteStream = Convert.ToBase64String(arr);
      //In this case the value of dataByteStream will be "AQI="
      This string is recieved in my C++ COM Server in a function called NotifyData(BSTR myDataByteStrea m). The .Net Component basically calls on one of the interface functions ( NotifyData(BSTR myData) ) of Com server by passing this string as argument. I am currently implementing the function NotifyData in COM Server. I want to now extract the byte array out of this string in C++. My objective is to get back the byte array with elements 1 and 2 which is the original data in my COM Server.
      My function in COM Server looks like this:
      Code:
      NotifyData(BSTR myData)
      {
      wstring myDataBytes = myData; //base64 string containing "AQI="
      
      byte *pArr = new byte[2];
      memset(pArr ,0,2);
      
      //Do something to get the original byte array into pArr such that pArr[0] = 1 and pArr[1] = 2
      
      }

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        The home for technical questions and answers at Microsoft. Get started asking, answering, and browsing questions about products like .Net, Azure, or Teams.


        Have you seen this?

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          This is not an answer. just a discussion

          Why did you declare
          Code:
          NotifyData(BSTR myData)
          myData as BSTR. cant you just define as string? cause in the calling function you are using string to store base64 data

          besides Base64 encoded data only contain ASCII data not multibyte data is present there. To understand base64 please check base64 RFC.

          even though wstring will not change the data that would be passed to NotifyData. but (I am not sure) wouldnt it make type casting error?

          by the way what is wstring?
          in visual C i have used wchar_t. this is 16bit in wchar_t data is stored as two byte. say if you store 'a' to wchar_t it would be equivalent to "0a"(char[2]).

          Comment

          • hype261
            New Member
            • Apr 2010
            • 207

            #6
            Johny,

            wstring is just a typedef of std::basic_stri ng<wchar_t>

            where string is just a typedef of std::basic_stri ng<char>

            Comment

            Working...