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

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds