By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Sep 24 '10 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 2,396
Please clarify what you want to do. It would help if you provided an example showing the expected output for a particular input value.
Sep 24 '10 #2

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1.  //.Net code
  2.   byte[] arr = new byte[2];
  3.   arr[0] = 1;
  4.   arr[1] = 2;
  5.   string dataByteStream = Convert.ToBase64String(arr);
  6. //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 myDataByteStream). 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:
Expand|Select|Wrap|Line Numbers
  1. NotifyData(BSTR myData)
  2. {
  3. wstring myDataBytes = myData; //base64 string containing "AQI="
  4.  
  5. byte *pArr = new byte[2];
  6. memset(pArr ,0,2);
  7.  
  8. //Do something to get the original byte array into pArr such that pArr[0] = 1 and pArr[1] = 2
  9.  
  10. }
Sep 24 '10 #3

ashitpro
Expert 100+
P: 542

100+
P: 1,059
This is not an answer. just a discussion

Why did you declare
Expand|Select|Wrap|Line Numbers
  1. 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]).
Sep 24 '10 #5

100+
P: 207
Johny,

wstring is just a typedef of std::basic_string<wchar_t>

where string is just a typedef of std::basic_string<char>
Sep 24 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.