STL Compatible Base64 Encoder

Environment: Any C++ compiler with STL support

This is compatible with STL base64 encoder.

It can encode/decode data from/to i(o)streams, streambufs, memory buffers, etc. Encoder uses iterator concept. Iterator must overload the *, ==, !=, and prefics ++ operators.

See encoder using:

1) Declare encoder

#include <base64.hpp>

int _State = 0;
base64<char> encoder; // base64 output/input in chars 
                      // (may be wchar_t, ...) or base64<> encoder;

2) Encode data form cin stream to output file

#include <iostream>
#include <fstream>

//
   ...
//
// encode from cin

istream& istr = cin;
istreambuf_iterator<char> _From(istr.rdbuf());
istreambuf_iterator<char> _To(0);

//_From, _Last, _To may be any iterator class or pointers

// encode to output file
ofstream ostr("test_out.txt");
ostreambuf_iterator<char> _Out(ostr);

// Encode data with CRLF ta the end of line.
// You can use base64<>::noline() or base64<>::crlfsp()
// or other classes.
encoder.put(_From, _To, _Out, _State, base64<>::crlf());

3) Decode data form file stream to console output stream

// decode from file ...
ifstream istr("test_out.txt");
istreambuf_iterator<char> _From(istr.rdbuf());
istreambuf_iterator<char> _To(0);

// ... to console
ostreambuf_iterator<char> _Out(cout);

encoder.get(_From, _To, _Out, _State);

Downloads

Download demo project - 5 Kb


Comments

  • whar_t support broken ?

    Posted by Legacy on 11/13/2003 12:00am

    Originally posted by: Vimal Earnest

    Hi,
    This is what I understood
    base64<char> encoder; //encode/decode 'char' type
    base64<wchar_t> encoder; //encode/decode 'wchar_t' type

    But the code given below doesn't work as expected

    int _State = 0;
    base64<wchar_t> encoder;
    {
    wchar_t buff_in[6] = L"Hello";
    wchar_t buff_out[12] = {0};
    encoder.put(buff_in, buff_in+6, buff_out, _State, base64<>::crlf());
    int len = wcslen(buff_out);
    encoder.get(buff_out, buff_out+len, buff_in, _State);
    assert(wcscmp(buff_in, L"Hello") != 0);
    }

    Is this a bug ?

    ~Vimal

    Reply
  • Good class, but...

    Posted by Legacy on 08/28/2002 12:00am

    Originally posted by: Lionel Zhang

    in get(), shouldn't _To be incremented?
    
    

    i.e., should it look like

    // write to output 3 bytes
    *_To++ = (byte_t) _3to4.get_0();
    *_To++ = (byte_t) _3to4.get_1();
    *_To++ = (byte_t) _3to4.get_2();

    instead of

    *_To = (byte_t) _3to4.get_0();
    *_To = (byte_t) _3to4.get_1();
    *_To = (byte_t) _3to4.get_2();

    there are also a few other lines in get() that need ++

    Reply
  • STL decode/Encode

    Posted by Legacy on 04/03/2002 12:00am

    Originally posted by: jyothi

    How to use ur code.I wanted to have c++ classes for encode and decode the base64 and quoted printable.
    Please help me out.

    Reply
  • Base64 codec

    Posted by Legacy on 03/06/2002 12:00am

    Originally posted by: Fanzhe Cui

    This is great code. It does not depend on any platform.
    Fanzhe

    Reply
  • STL Compatible Base64 Encoder - Excellent.

    Posted by Legacy on 01/26/2002 12:00am

    Originally posted by: James Pannozzi

    Excellent utilization of STL.

    Would it be easy to modify it to decode MHTML (.mht) into text and jpg images?

    Thanks again for interesting code.

    Ochen Xoposho!

    Jim

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds