Hi,
I am trying to get TOTP-Arduino (GitHub - lucadentella/TOTP-Arduino) library working. It works fine when I generate the byte array outside of Arduino, but the goal is to get the secret from a sqlite database/spiffs and then get the TOTP token.
If you mean what I think that you mean, I think you're wrong. The variable that holds the pointer to dynamically allocated memory goes out of scope; but the memory itself stays allocated.
#include <MemoryFree.h>
String secret = "SuperLongSecret";
void setup()
{
Serial.begin(57600);
Serial.println("'new' test");
Serial.print("len = ");
Serial.println(secret.length());
}
void loop()
{
char * hmacKey = new char [secret.length() + 1];
strcpy (hmacKey, secret.c_str());
Serial.print(F("Free RAM = ")); //F function does the same and is now a built in library, in IDE > 1.0.0
Serial.println(freeMemory(), DEC); // print how much RAM is available.
delay(1000);
if (Serial.available())
{
Serial.println("stopping");
for (;;);
}
}
Just create a char array directly instead of a pointer to a char array, the compiler has no problem with a dynamically sized char array inside a function.
Thanks for all the feedback and discussion, I am afraid I am still a bit lost. Now the string is a char, but how do I change it into hex array? I think I am more confused than before now
The C string is a char array with ASCII text codes (one symbol per char -- letter, number, punctuation, other) that ends with a char == 0.
char oneHundredText[ 4 ] = "100"; // chars '1', '0', '0', 0. The ones in ' ' are print literals, like marks on paper 1 0 0 three different things one after the other.
If I have a text number like "1234", I can read each char in a loop and add them in an int.
not tested at all:
int total; // default is 0
byte index; // to point into the number array
char number[ ] = "1234";
... snip ....
while ( number[ index ] > 0 )
{
total *= 10; // total = total * 10
total += number[ index++ ] - '0'; // assumes digits only
}
print total; // there's your integer, floating point is trickier to get right
bert2002:
Thanks for all the feedback and discussion, I am afraid I am still a bit lost. Now the string is a char, but how do I change it into hex array? I think I am more confused than before now
I can't be sure of what these terms mean to you, only guess.
string is never --- a char
string is a series of chars in an array with a char == 0 to mark the end.
If you want chars which are signed 8-bit values to become bytes which are unsigned 8-bit values you just call then that by casting or define a pointer to byte and point it at the string to read bytes instead of chars... this is about making the compiler treat data differently than it was defined as, --- but the data is the same, just treated differently, no converting needed!
I suggest that you study variables in C.
Here's a page from the Arduino main site with links you could resolve a lot of confusion through, including variables.