473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
5 9656
donbock
2,426 Expert 2GB
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
abeda
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
542 Expert 512MB
http://social.msdn.microsoft.com/for...5-0e00230033fb

Have you seen this?
Sep 24 '10 #4
johny10151981
1,059 1GB
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
hype261
207 100+
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

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

Similar topics

1
by: rawCoder | last post by:
Hi, Ok this shud b simple but i cant seem to find it. I want to convert a System.UInt16 data to 2 Byte array and System.UInt32 data to 4 Byte array ( BigEndian) something like Public Sub...
3
by: David | last post by:
I need to covert DateTime Variables into a byte array but all of the conversion methods do not appear to handle the DateTime type. Is there a generic way to convert any property or object into a...
1
by: Paul W | last post by:
How do I do this with: Option Strict On? I know of the function System.Text.ASCIIEncoding.GetChars, but that takes a Byte array not a DataRow. So with Option Strict On, how do I take the byte...
9
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
2
by: Nikolay Petrov | last post by:
How can I convert variable of type Object to Byte array? TIA
2
by: Michael White | last post by:
I have an icon stored as a resource in my project. Once getting the icon using My.Resources.ResourceManager.GetObject(IconName) How can I convert it to a byte array? Thanks.. Michael...
8
by: piggy | last post by:
I am trying to convert an image to an byte array (tobytes() method) and from byte array back to an image (frombytes() method). The problem i have here is say the image size is 285 bytes but the...
7
by: mehafi | last post by:
hi, I need to write a few integer numbers to file. A method write from FileStream writes an array of bytes. So how to conwert my integers to byte array? I've tried Convert class, but it hasn't...
1
by: Christian Reizlein | last post by:
How do i convert an integer value to a byte array?
5
by: da1978 | last post by:
Hi experts, I need to convert a string or a Byte array to a string byte array. Its relatively easy to convert a string to an char array or a byte array but not a STRING byte array. i.e. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.