Giter VIP home page Giter VIP logo

base64_arduino's Introduction

base64_arduino

Base64 encoder/decoder for arduino repo

npm Build Status

Installation

Add base64.cpp and base64.hpp to your project folder or library search path, put #include "base64.hpp" in your source, and pass base64.cpp to your compiler

Usage (Arduino)

There is an example Arduino sketch in the arduino-test folder.

Usage (libc)

Binary to base64 example:

unsigned char binary[] = {133, 244, 117, 206, 178, 195};
unsigned char base64[9]; // 8 bytes for output + 1 for null terminator

unsigned int base64_length = encode_base64(binary, 6, base64);

printf("%d\n", base64_length); // Prints "8"
printf((char *) base64); // Prints "hfR1zrLD"

Base64 to binary example:

unsigned char base64[] = "hfR1zrLD";
unsigned char binary[6];

unsigned int binary_length = decode_base64(base64, binary);

printf("%d\n", binary_length); // Prints "6"
printf("[%d, %d, %d, %d, %d, %d]\n", // Prints "[133, 244, 117, 206, 178, 195]"
       binary[0], binary[1], binary[2],
       binary[3], binary[4], binary[5]);

String to base64 example:

unsigned char string[] = "String example";
unsigned char base64[21]; // 20 bytes for output + 1 for null terminator

// encode_base64() places a null terminator automatically, because the output is a string
unsigned int base64_length = encode_base64(string, strlen((char *) string), base64);

printf("%d\n", base64_length); // Prints "20"
printf((char *) base64); // Prints "U3RyaW5nIGV4YW1wbGU="

Base64 to string example:

unsigned char base64[] = "U3RyaW5nIGV4YW1wbGU=";
unsigned char string[15]; // 14 bytes for output + 1 for null terminator

// decode_base64() does not place a null terminator, because the output is not always a string
unsigned int string_length = decode_base64(base64, string);
string[string_length] = '\0';

printf("%d\n", string_length); // Prints "14"
printf((char *) string); // Prints "String example"

Details

Uses common web conventions - '+' for 62, '/' for 63, '=' for padding. Note that invalid base64 characters are interpreted as padding.

Can be compiled as C, uses .*pp extensions because it is usually used in C++ projects and is tested for C++.

License

MIT

base64_arduino's People

Contributors

densaugeo 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.