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
Solved

C++ Base64 to byte array

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 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