[SOLVED] Char variable not availabe in void loop(); Base64 library

Hello,

Can someone please explain to me why the variable encodedString is only available in void setup()?
How can i make it available in void loop() ?

Thank you very much.

#include <Base64.h>

void setup()
{
  // start serial port at 115200 bps:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB only
  }

  // encoding
  char inputString[] = "Base64example";
  int inputStringLength = sizeof(inputString);

  Serial.print("Input string is:\t");
  Serial.println(inputString);

  Serial.println();

  int encodedLength = Base64.encodedLength(inputStringLength);
  char encodedString[encodedLength];
  Base64.encode(encodedString, inputString, inputStringLength-1);
  Serial.print("Encoded string is:\t");
  //Serial.println(encodedString);
}


void loop() {
  Serial.println(encodedString);
}

It’s a scope issue.
A variable defined in a function is not visible in another function.
Move it outside both functions, to make it global.

When i move the code lines outside the void setup() function, i get the following error:

exit status 1
array bound is not an integer constant before ‘]’ token

#include <Base64.h>

 char inputString[] = "  Base64example 123";
 int inputStringLength = sizeof(inputString);

 int encodedLength = Base64.encodedLength(inputStringLength);
 char encodedString[encodedLength];
 Base64.encode(encodedString, inputString, inputStringLength-1);

void setup()
{
  // start serial port at 115200 bps:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB only
  }

  // encoding

  //Serial.println(encodedString);
}


void loop() {
  Serial.println(encodedString);
}

When you declare an array the length must be a constant.

How can i use the value of encodedString in the void loop() function?
Thank you.

Your program has to successfully compile, and it won't until you follow the advice in reply #3.

I have tryied doing the following but i still get the same error message.

exit status 1
array bound is not an integer constant before ‘]’ token

#include <Base64.h>
  char inputString[] = "Base64example 123";
  const int inputStringLength = sizeof(inputString);
  const int encodedLength = Base64.encodedLength(inputStringLength);
  char encodedString[encodedLength];
  Base64.encode(encodedString, inputString, inputStringLength-1);
  Serial.print("Encoded string is:\t");

void setup()
{
  // start serial port at 115200 bps:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB only
  }

  // encoding


  Serial.print("Input string is:\t");
  Serial.println(inputString);

  Serial.println();


  //Serial.println(encodedString);
}


void loop() {
  Serial.println(encodedString);
}

You are mixing the varaible declaration and the code.

Serial.print isn’t going to compile outside of a function.

Don’t be tempted to use sizeof to get the length of a string, use strlen.

Thank you for your help.
I understand that Serial.print() does not work outside of a function.

However even using strlen i get the error message:
exit status 1
array bound is not an integer constant before ‘]’ token

#include <Base64.h>
  
  char inputString[] = "Base64example 123";
  const int inputStringLength = strlen(inputString);
  const int encodedLength = Base64.encodedLength(inputStringLength);
  char encodedString[encodedLength];
  Base64.encode(encodedString, inputString, inputStringLength-1);

void setup()
{
  Serial.begin(115200);
}


void loop() {
  Serial.println(encodedString);
}

Did that initial code compile correctly?

The reason I ask is that it seems to defy the rule "When you declare an array the length must be a constant." given in post 3 as encodedLength is calculated on the fly?

paulie_iwin:
Thank you for your help.
I understand that Serial.print() does not work outside of a function.

Neither does Base64.encode

countrypaul:
You are mixing the varaible declaration and the code.

The initial code is the following and it compiles correctly. I think i have failed to publish this in post #1. :frowning:
I see that with strlen i do not need to add -1 to inputStringLength.
Thank you for the tip!

#include <Base64.h>

void setup()
{
  // start serial port at 115200 bps:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB only
  }

  // encoding
  char inputString[] = "Base64EncodeExample";
  int inputStringLength = strlen(inputString);
  int encodedLength = Base64.encodedLength(inputStringLength);
  char encodedString[encodedLength];
  Base64.encode(encodedString, inputString, inputStringLength);
  Serial.println(encodedString);
}


void loop() {
//Serial.println(encodedString);
}

Hello again,

I made it work by placing the code inside a String function.

#include <Base64.h>

String myString()
{
  // encoding
  char inputString[] = "Base64EncodeExample";
  int inputStringLength = strlen(inputString);
  int encodedLength = Base64.encodedLength(inputStringLength);
  char encodedString[encodedLength];
  Base64.encode(encodedString, inputString, inputStringLength);

  return(encodedString);
}
String myEncode = myString();

void setup()
{
  // start serial port at 115200 bps:
  Serial.begin(115200);
}


void loop() {
Serial.println(myEncode);
}

paulie_iwin:
How can i use the value of encodedString in the void loop() function?
Thank you.

Define your global as..

char* myString;   // Globally you create just a char pointer..

void setup(void) {

   ...
   ...
   ... // bla bla bla
   int caculatedStringLength = something..  // Now you know how big it must be, so..
   myString = (char*) malloc(caculatedStringLength+1);   // Give an extra byte for the'\0'
   ...
}

The pointer was defined as a global, so you can access it the same as a regular global. AND it acts exactly the same as the array of chars that you wanted in the first place.

-jim lee

String myString()
{
  // encoding
  char inputString[] = "Base64EncodeExample";
  int inputStringLength = strlen(inputString);
  int encodedLength = Base64.encodedLength(inputStringLength);
  char encodedString[encodedLength];
  Base64.encode(encodedString, inputString, inputStringLength);

  return(encodedString);
}

You should never return a pointer to a local variable in a function. Once the function exits that memory can get written over.

ToddL1962:
You should never return a pointer to a local non-static variable in a function. Once the function exits that memory can get written over.

TheMemberFormerlyKnownAsAWOL:

Doesn't appear to be solved...but whatever. LOL

ToddL1962:

String myString()

{
 // encoding
 char inputString = “Base64EncodeExample”;
 int inputStringLength = strlen(inputString);
 int encodedLength = Base64.encodedLength(inputStringLength);
 char encodedString[encodedLength];
 Base64.encode(encodedString, inputString, inputStringLength);

return(encodedString);
}




You should never return a pointer to a local variable in a function. Once the function exits that memory can get written over.

That function returns a String object, not a pointer to an automatic variable. The encodedString array is converted to a String in the return statement.

Question for OP: doesn’t encodedString need to be null-terminated? That means it needs to be encodedLength+1 chars long, not just encodedLength chars long (unless Base64.encodedLength() already adds 1 for you).

christop:
That function returns a String object, not a pointer to an automatic variable. The encodedString array is converted to a String in the return statement.

Question for OP: doesn't encodedString need to be null-terminated? That means it needs to be encodedLength+1 chars long, not just encodedLength chars long (unless Base64.encodedLength() already adds 1 for you).

Subtle point and well-taken. encodedString is a pointer to the dynamically created encodedString array, but I suppose it is copied into the string object before it goes out of scope. However, I suspect given the apparent skill of the OP it was a fortunate coincidence it works. Had the return type been a char * it would have been a coding error.