Giter Site home page Giter Site logo

base-n's Introduction

base-n provides encoding/decoding support for BaseX encoding schemes accessible through a standard STL-like iterator interface. Standard Base16, Base32, and Base64 are available out-of-the-box. Adding or modifying custom schemes is supported.

Usage overview

base-n is a small, single-header library which provides standard Base16, Base32, Base64, and custom Base-N encoding support.

The main functionality is delivered by the following functions in bn namespace:

template<class Iter1, class Iter2>
void encode_b16(Iter1 start, Iter1 end, Iter2 out);


template<class Iter1, class Iter2>
void encode_b32(Iter1 start, Iter1 end, Iter2 out);


template<class Iter1, class Iter2>
void encode_b64(Iter1 start, Iter1 end, Iter2 out);


template<class Iter1, class Iter2>
void decode_b16(Iter1 start, Iter1 end, Iter2 out);


template<class Iter1, class Iter2>
void decode_b32(Iter1 start, Iter1 end, Iter2 out);


template<class Iter1, class Iter2>
void decode_b64(Iter1 start, Iter1 end, Iter2 out);

In order to encode and decode data in std::string variable in, you can do the following:

bn::encode_b64(in.begin(), in.end(), back_inserter(encoded));
bn::decode_b64(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));

Should you find yourself lacking some encoding scheme or the default character mapping rules are not good for your use case, you can easily provide your own encoder. For that, you need to define a struct type which will describe the new encoding. Sample below:

struct b8_custom
{
        static size_t group_length()
        {
                return 3;
        }

        static char encode(int index)
        {
                const char* const dictionary = "01234567";
                assert(index < strlen(dictionary));
                return dictionary[index];
        }

        static char decode(char c)
        {
                if (c >= '0' && c <= '7') {
                        return c - '0';
                }
                return -1;
        }
};
...
string encoded;
bn::impl::encode<b8_custom>(in.begin(), in.end(), back_inserter(encoded));
bn::impl::decode<b8_custom>(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));

For a full working example, see basen_example.cpp file in example directory.

base-n's People

Contributors

azawadzki avatar cedricguillemet avatar vikkov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.