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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds