Solved

C++ Base64 to byte array

Posted on 2009-05-15
5
1,758 Views
Last Modified: 2012-05-07
Hi everybody,

I have an application that transfers jpegs over a network. Basically, my old approach was to transfer decimal representations of the bytes separated by a delimiter, and then parse these into a byte array which I could show on a quartz view.

You can imagine, I'm sure, that this approach was not very efficient, and now I'm interested in transferring the image in Base64. However, I have no idea how to decode the Base64 into an array of bytes on the receiving end. I've looked around and all I've found is something that decodes the Base64 into an iostream string, but that's not very helpful for my purposes.

Does anyone have any ideas on what to do from here? Thanks a lot in advance!
0
Comment
Question by:ehensens
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 24397029
I'd suggest to - instead of resinventing the wheel - use a ready-made class for that, see e.g. http://www.codeproject.com/KB/cpp/base64coding.aspx ("Base64 Encoding And Decoding"). It's quite easy to use, and the article comes with full source code.
0
 

Author Comment

by:ehensens
ID: 24397561
This is good, I need to pass a wstring into the decoding function. How do I go from a char[] to a wstring?
0
 
LVL 86

Expert Comment

by:jkr
ID: 24398014
No need to change any thing the strings are already wstrings, see 'Managed.Ext.h':
#ifndef _MANAGED

	#include <string>

	using namespace std;
 

	#define CONST const

	#define CLASS class

	#define AS_PUBLIC

	#define NEW new

	#define NULLPTR NULL
 

	typedef wstring STRING; // <------------- !

	typedef const STRING& STRING_IN;

	typedef STRING& STRING_OUT;

	typedef const STRING& STRING_RET;

#else

	//...

#endif

Open in new window

0
 

Author Comment

by:ehensens
ID: 24398421
Right, but what I need decoded is a char array, so how do I get that to a wstring?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 24398512
Ah, got it ;o)

You can do that like
char arr[SOME_SIZE];

//...
 

size_t len = strlen(arr);

wchar_t* pwsz = new wchar_t[len + 1];

memset(pwsz,0,(len + 1) * sizeof(wchar_t));
 

mbstowcs(pwsz,arr,len);
 

// decode pwsz...
 

delete[] pwsz; // clean up

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now