Solved

C++ Base64 to byte array

Posted on 2009-05-15
5
1,876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

734 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