From 709d5ab8dd2abb053bdf05b9b412eee1082ccb73 Mon Sep 17 00:00:00 2001 From: palto42 <12280188+palto42@users.noreply.github.com> Date: Sun, 6 May 2018 16:24:46 +0200 Subject: [PATCH] corrected string lengths in example code --- examples/base64/base64.ino | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/base64/base64.ino b/examples/base64/base64.ino index 31c8e21..a6865d8 100644 --- a/examples/base64/base64.ino +++ b/examples/base64/base64.ino @@ -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. */ @@ -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);