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?
1 Answer 1
Arduino String
class provides method c_str()
. So you don't have to convert it to C string, as it's already stored as a C string internally.
And as mentioned in comments, the second parameter of strtoul
is:
endptr
Reference to an object of typechar*
, whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
Basically you can reduce whole foo to the: return strtoul(time.c_str(), NULL, 10);
-
Can you tell me more about how to access the
c_str()
method of a String? just usingsec=strtoul(tim,endptr,10);
did not work.ATE-ENGE– ATE-ENGE2017年09月18日 13:25:49 +00:00Commented Sep 18, 2017 at 13:25 -
@ATE-ENGE Added to the answer.KIIV– KIIV2017年09月18日 13:29:37 +00:00Commented Sep 18, 2017 at 13:29
-
2Also be aware that
strtol()
can return 0 or ULONG_MAX if there is a problem, so the calling function should check for these because if you do adelay(ULONG_MAX);
you'll think it has locked up :)Code Gorilla– Code Gorilla2017年09月18日 13:33:05 +00:00Commented Sep 18, 2017 at 13:33 -
@CodeGorilla That would be a problem! Hypothetically, is there a good way to differentiate if a problem occurs? You know, in case a user actually does want to halt the program for 50 days :)ATE-ENGE– ATE-ENGE2017年09月18日 13:37:15 +00:00Commented Sep 18, 2017 at 13:37
-
1@ATE-ENGE You can use that endptr to check if whole string was parsed. However it returns 0 if there is nothing to parse and ULONG_MAX if the value is out of range so it must be even bigger than 32bit number.KIIV– KIIV2017年09月18日 13:40:26 +00:00Commented Sep 18, 2017 at 13:40
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