Skip to content

Commit 74d8926

Browse files
committed
Adding a basic encode/decode example
1 parent 347aba3 commit 74d8926

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

examples/base64/base64.ino

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <Base64.h>
2+
3+
/*
4+
Base64 Encode/Decode example
5+
6+
Encodes the text "Hello world" to "SGVsbG8gd29ybGQA" and decodes "Zm9vYmFy" to "foobar"
7+
8+
Created 29 April 2015
9+
by Nathan Friedly - http://nfriedly.com/
10+
11+
This example code is in the public domain.
12+
13+
*/
14+
15+
16+
void setup()
17+
{
18+
// start serial port at 9600 bps:
19+
Serial.begin(9600);
20+
while (!Serial) {
21+
; // wait for serial port to connect. Needed for Leonardo only
22+
}
23+
24+
Serial.println("Base64 example");
25+
26+
27+
28+
// encoding
29+
char input[] = "Hello world";
30+
int inputLen = sizeof(input);
31+
32+
int encodedLen = base64_enc_len(inputLen);
33+
char encoded[encodedLen];
34+
35+
Serial.print(input); Serial.print(" = ");
36+
37+
// note input is consumed in this step: it will be empty afterwards
38+
base64_encode(encoded, input, inputLen);
39+
40+
Serial.println(encoded);
41+
42+
43+
44+
// decoding
45+
char input2[] = "Zm9vYmFy";
46+
int input2Len = sizeof(input2);
47+
48+
int decodedLen = base64_dec_len(input2, input2Len);
49+
char decoded[decodedLen];
50+
51+
base64_decode(decoded, input2, input2Len);
52+
53+
Serial.print(input2); Serial.print(" = "); Serial.println(decoded);
54+
}
55+
56+
57+
void loop()
58+
{
59+
60+
}

0 commit comments

Comments
 (0)