I'm facing some frustrating issue and I'm not able to figure it out. I know there are some topics similar to this one but not with the particularity of strtok (I think should be the problem).
I have this code:
unsigned long longintvalue = 0;
char * strtokIndx; // this is used by strtok() as an index
... // Here I'm getting different strings tokens: char, int, float... without problem.
strtokIndx = strtok(NULL, ":");
Serial.print("ATOL: ");
Serial.println(strtokIndx); // this string is correct, for example. 1921231231231
longintvalue = atol(strtokIndx); // convert this part to a long int
Serial.print("LONG: ");
Serial.println(PriUint64<DEC>(longintvalue));
Well, I have tried: atol(), strtol()... but always I get the maximum value of float value (2147483647).
I'm not able to convert this string into a long int value.
It works for int, float, char... but not this one.
it seems the Arduino has neither the ability to print (i.e. format) a long long value and doesn't have the ability to translate a string to a long long value (i.e. atoll ()).
He's defined the value as an unsigned long and his example number is > 97 billion when the max is 4 billion. @gcjr: I think you're right that the IDE cannot print a long long value.
Thanks @gcjr!
I will try, of course (even If I'm not fully desperate) I thought in some function ad hoc for the purpose, but I tried to take advantage of some function... bad luck.
static char * lltoa ( long long value, char *str, unsigned radix)
{
char *ptr = str;
// Deal with negativity.
if ( value < 0 )
{
*ptr++ = '-';
value = ( uint64_t ) ( - ( int64_t ) value );
}
char *first = ptr; // Pointer to first digit.
do
{
unsigned digit = ( unsigned ) ( value % radix );
value /= radix; // Trick, repeatedly divide by radix!
// Convert to ASCII.
if ( digit < 10 )
*ptr++ = ( char ) ( digit + '0' ); // A digit. '0'..'9'.
else
*ptr++ = ( char ) ( digit - 10 + 'A' ); // A letter. 'A'..'Z'.
}
while ( value > 0 );
// Now 'ptr' points to the reverse string of digits that represents 'value'.
// So, reverse the string. In Digital Mars C++ (Symantec C++, Zortech C++)
*ptr-- = '\0'; // ptr points to first, p points to last.
do
{
// Swap em!
char help = *ptr;
*ptr = *first;
*first = help;
--ptr; // Move on forward and reverse!
++first;
}
while ( first < ptr ); // Only go halfway!
return ( str );
}
void setup() {
int i;
Serial.begin(9600);
long long x=123456789LL;
char buf[20];
Serial.println(lltoa(x,buf,10));
}
void loop() {}