Solved

C++ Base64 to byte array

Posted on 2009-05-15
5
1,707 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 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

17 Experts available now in Live!

Get 1:1 Help Now