Click here to Skip to main content
12,297,421 members (60,848 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked


, 13 Jun 2001
Rate this:
Please Sign up or sign in to vote.
An article on how to decode Base64 and Quoted-Printable text without using MFC.


There are already other articles here on The Code Project that shows how to decode Base64 and Quoted-Printable, but they all use MFC. I needed some code that didn't use MFC, so I wrote AMMimeUtils.

I wrote these classes because I was working with receiving and sending emails and Usenet messages. Almost all email messages and attachments are either Base64 or Quoted-Printable encoded. Attachments in Usenet messages are often UU encoded, I still need to write a class to handle this, but it might come in a later version.

When you get an email, the subject and other header fields might also be encoded, so this code also includes some code to decode these fields. Different mail programs encode the subject in different ways. The following text:

Just a small text (for demo), and some more text...

can look both like

=?iso-8859-1?Q?Just a small text =28for demo=29, and some more text...?=

or like

Just a small text =?iso-8859-1?Q?=28for demo=29?=, and some more text...

The first line is easy, because we can see that the entire string is encoded with Quoted-Printable (the ?Q? part means Quoted-Printable). In the second string, it's only a part of it that's encoded, so we have to get the first non-encoded part, decode the encoded part, and get the last non-encoded part, and add the 3 parts together to get the final subject.

I made a function char* MimeDecodeMailHeaderField(char *s); to handle this. If you have a string called s containing the subject you want to decode, simply call it like this:

s = MimeDecodeMailHeaderField(s);

Now s contains the decoded text.

I have 2 classes CBase64Utils and CQPUtils for general encoding and decoding of Base64 and Quoted-Printable. The interface looks like:

//class to handle all base64 stuff...
class CBase64Utils
  int ErrorCode;
  int GetLastError() {return ErrorCode;};
  //caller must free the result, bufsize holds the decoded length
  char* Decode(char *input, int *bufsize);
  //caller must free the result, bufsize is the length of the input buffer
  char* Encode(char *input, int bufsize);

//class to handle quoted-printable stuff
class CQPUtils
  char* ExpandBuffer(char *buffer, int UsedSize, 
             int *BufSize, bool SingleChar = true);
  int ErrorCode;
  int GetLastError() {return ErrorCode;};
  char* Decode(char *input); //caller must free the result
  char* Encode(char *input); //caller must free the result

The only difference is the Decode() and Encode() functions. Quoted-Printable is always text, therefore it only takes one parameter, the string containing encoded text, and returns a pointer to a new buffer containing the decoded text. Base64 might be an encoded binary file, so it puts the length of the returned buffer in the bufsize variable. Then it's possible to save the decoded buffer as a binary file.

Both classes have a function GetLastError(), if you decode something, and this variable is zero, everything is fine, if it's non-zero there was an error in the input, but you still get the (maybe) encoded/decoded result.

Right now, this code only has functions for what I needed when I wrote it. In the future, it I might add some better error handling.

If you want to know more about MIME and email messages, you can take a look at:

  • RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One, Format of Internet Message Bodies
  • RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two, Format of Internet Message Bodies
  • RFC 2044 - Multipurpose Internet Mail Extensions (MIME) Part Three, Format of Internet Message Bodies


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Anders Molin
Software Developer (Senior)
Denmark Denmark
Huh! Wink | ;-)

You may also be interested in...

Comments and Discussions

BugMajor bug Pin
Pavel6729-Mar-12 4:06
memberPavel6729-Mar-12 4:06 
Generalunicode version Pin
mengkim3-Apr-07 22:39
membermengkim3-Apr-07 22:39 
GeneralNotice!!! Pin
songzd7-Dec-06 22:51
membersongzd7-Dec-06 22:51 
GeneralBug in mail decoder Pin
superagenteg22-Nov-06 23:24
membersuperagenteg22-Nov-06 23:24 
QuestionBUG when decode b64 HTML!? Pin
KFC1234-Jul-06 21:45
memberKFC1234-Jul-06 21:45 
GeneralSmall, but nasty bugs :mad: Pin
scjurgen16-Sep-04 2:41
memberscjurgen16-Sep-04 2:41 
General2 bugs found Pin
David Connet28-Apr-04 13:45
memberDavid Connet28-Apr-04 13:45 
GeneralRe: 2 bugs found Pin
Nate Austin8-Mar-07 18:53
memberNate Austin8-Mar-07 18:53 
GeneralQ-Printable decode improvement Pin
Irek Zielinski13-Apr-04 8:53
memberIrek Zielinski13-Apr-04 8:53 
GeneralRe: Q-Printable decode improvement Pin
thenickname19-Jan-05 9:55
sussthenickname19-Jan-05 9:55 
I'm not sure I would call that an improvement. The MIME RFC specifies that the quoted printable characters must always be in uppercase ("Uppercase letters must be used; lowercase letters are not allowed." / RFC 20459). So using toupper would violate this requirement and hence make your implementing a bit buggier..
GeneralRe: Q-Printable decode improvement Pin
Irek Zielinski19-Jan-05 10:21
memberIrek Zielinski19-Jan-05 10:21 
GeneralI think it is bug (base64 encode) Pin
KiangGiap Lau8-Jan-04 16:52
memberKiangGiap Lau8-Jan-04 16:52 
GeneralRe: I think it is bug (base64 encode) Pin
ratzfatz18-Jan-04 23:27
memberratzfatz18-Jan-04 23:27 
Generaldecoding string with many ISO Pin
DevCrazy17-Jun-03 4:01
memberDevCrazy17-Jun-03 4:01 
GeneralBinary data Pin
Juan Carlos Cobas5-Sep-02 23:10
memberJuan Carlos Cobas5-Sep-02 23:10 
GeneralRe: Binary data Pin
Irek Zielinski9-Apr-04 7:29
memberIrek Zielinski9-Apr-04 7:29 
GeneralGet Base64 Encoded Length Pin
Paul Kissel22-Aug-02 8:25
memberPaul Kissel22-Aug-02 8:25 
GeneralRe: Get Base64 Encoded Length Pin
Paul Kissel22-Aug-02 8:49
memberPaul Kissel22-Aug-02 8:49 
GeneralRe: Get Base64 Encoded Length Pin
Anders Molin26-Aug-02 13:55
memberAnders Molin26-Aug-02 13:55 
Generalquoted-printable result truncated. Pin
Anonymous2-Nov-01 13:06
memberAnonymous2-Nov-01 13:06 
GeneralRe: quoted-printable result truncated. Pin
Anonymous5-Nov-01 7:23
memberAnonymous5-Nov-01 7:23 
GeneralRe: quoted-printable result truncated. Pin
Anders Molin17-Nov-01 9:30
memberAnders Molin17-Nov-01 9:30 
GeneralBug in decode Pin
Anonymous25-Oct-01 4:55
memberAnonymous25-Oct-01 4:55 
QuestionIn the class CBase64,why the decode fun can't decode the jpg file whice had been encode?? Pin
Anonymous17-Oct-01 17:02
memberAnonymous17-Oct-01 17:02 
AnswerRe: In the class CBase64,why the decode fun can't decode the jpg file whice had been encode?? Pin
Anders Molin19-Oct-01 10:09
memberAnders Molin19-Oct-01 10:09 
GeneralBug with resultlen of Decode Pin
STF17-Oct-01 5:25
memberSTF17-Oct-01 5:25 
GeneralBug in Base64 encoding Pin
CRahul26-Sep-01 5:01
memberCRahul26-Sep-01 5:01 
GeneralRe: Bug in Base64 encoding Pin
Anders Molin26-Sep-01 7:45
memberAnders Molin26-Sep-01 7:45 
GeneralLinks to the RFC documents Pin
Uwe Keim14-Jun-01 22:49
memberUwe Keim14-Jun-01 22:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 14 Jun 2001
Article Copyright 2001 by Anders Molin
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid