STL Compatible Base64 Encoder | CodeGuru

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 int _State = 0; base64 encoder; // base64 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 31, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.