Skip to content

corrected string lengths in example code #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions examples/base64/base64.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
/*
Base64 Encode/Decode example

Encodes the text "Hello world" to "SGVsbG8gd29ybGQA" and decodes "Zm9vYmFy" to "foobar"
Encodes the text "Hello world" to "SGVsbG8gd29ybGQ=" and decodes "Zm9vYmFy" to "foobar"

Created 29 April 2015
by Nathan Friedly - http://nfriedly.com/

updated by palto42 https://github.com/palto42/

This example code is in the public domain.

*/
Expand All @@ -27,26 +28,25 @@ void setup()

// encoding
char input[] = "Hello world";
int inputLen = sizeof(input);
int inputLen = sizeof(input) - 1; // don't count the trailing \0 of the string
// note that the runtime function "strlen()" doesn't count the terminating \0

int encodedLen = base64_enc_len(inputLen);
char encoded[encodedLen];

Serial.print(input); Serial.print(" = ");
char encoded[encodedLen + 1]; // add +1 for \0 string termination

// note input is consumed in this step: it will be empty afterwards
base64_encode(encoded, input, inputLen);

Serial.println(encoded);
Serial.print(input); Serial.print(" = "); Serial.println(encoded);



// decoding
char input2[] = "Zm9vYmFy";
int input2Len = sizeof(input2);
int input2Len = sizeof(input2) - 1; // don't count the trailing \0 of the string

int decodedLen = base64_dec_len(input2, input2Len);
char decoded[decodedLen];
char decoded[decodedLen + 1]; // add +1 for \0 string termination

base64_decode(decoded, input2, input2Len);

Expand Down