I'm trying to use strtoul()
to convert an Arduino String to an unsigned long (like the return of millis();
) using the following code snippet:
unsigned long foo (String time)
{
unsigned long mili;
char Tim[9]="";
uint16_t timsize = time.length()+1;
char TIM[timsize];
time.toCharArray(TIM,timsize);
mili=strtoul(TIM,timsize,10);
return mili;
}
The code seems to work. However, I'm getting the following warning:
invalid conversion from 'uint16_t {aka unsigned int}' to 'char**' [-fpermissive]
mili=strtoul(TIM,timsize,10);
My question is twofold:
Is there a better way to convert a String to unsigned long without first converting to a C string?
If not, how can I eliminate this error?
unsigned long strtoul(const char *str, char **str_end, int base );
The 2nd parameter ischar **
. Can benullptr
.char TIM[timsize]
is bad code. The size of arrays need to be know at compile time and since the value oftime
is not constant its length can't be known. I think you'd need to do anew
, if you were still doing it which you aren't because you are using.c_str()
char TIM[timsize]
usually works on newer compilers: Declaring an array with a non-constant size variable