
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Arduino – base64 encode and decode
Arduino contains a library that helps with base64 encode and decode. You can download it from the Library Manager. Search for base64, and install the library by Densaugeo.
Now, open a new sketch and run the following sample code −
#include "base64.hpp" unsigned char normal_text[20] = "Hello World"; unsigned char base64_text[20]; unsigned char decoded_text[20]; void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); int base64_length = encode_base64(normal_text,12,base64_text); Serial.print("Base64 Text: ");Serial.println((char *) base64_text); Serial.print("Base64 Length: ");Serial.println(base64_length); int decoded_length = decode_base64(base64_text,decoded_text); Serial.print("Decoded Text: ");Serial.println((char *)decoded_text); Serial.print("Decoded Length: ");Serial.println(decoded_length); } void loop() { // put your main code here, to run repeatedly: }
Output
The Serial Monitor Output is shown below −
You can verify this output on websites like: https://base64.guru/converter/decode
As you can see, this library deals with unsigned character arrays. The base64_encode function takes three arguments −
The array to be converted
The number of elements in the array to be converted
The array in which to store the encoded values
It returns the length of the encoded array.
The base64_decode function takes in two arguments −
The array containing the encoded values
The array in which to store the decoded results
Since Serial.print() and its variations don't deal with unsigned char arrays, you need to cast it to a char array for printing.
- Related Questions & Answers
- Encode and Decode Strings in C++
- Understanding base64 encode in MySQL?
- Encode and decode uuencode files using Python
- Encode and decode binhex4 files using Python (binhex)
- Encode and decode XDR data using Python xdrlib
- How to Encode and Decode JSON and Lua Programming?
- Encode and decode MIME quoted-printable data using Python
- How to encode and decode a URL in JavaScript?
- What is the difference between encode/decode in Python?
- Node.js – Base64 Encoding & Decoding
- PHP – Encode string for MIME header using mb_encode_mimeheader()
- AVR libraries in Arduino – Introduction
- WiFi with Arduino – Scan Networks
- PHP – Decode multiple MIME header fields at once using iconv_mime_decode_headers()
- Arduino IDE 2.0 – Using the Boards Manager