Solved

C++ Base64 to byte array

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Global Keyboard Hooks Blocked 4 88
If a cell in a range equals "YES" return specific Test. 4 42
Please explain C language error codes ? 5 43
MySQL recovery 7 29
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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

756 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