Convert with atol() string to long int after strtok

Hello everybody,

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.

Any idea about whats happening?

Thank you all.

What's this all about?

PriUint64<DEC>

What was wrong with...

Serial.println(longintvalue);

Serial.println() can't print long int values.
This is a library for that purpose. Anyway, if I force something like that:

longintvalue = 97184158521;
Serial.println(PriUint64(longintvalue));

The outout is perfect.

If I try to print values as you said, I get the same issue:

Thanks for the reply!

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 ()).

i hope someone can prove me wrong

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.

consider (if desperate)

results

jolopez: 1923123123123
jolopez: 1900000000003
jolopez: 1
jolopez: 12
jolopez: 123
// -----------------------------------------------------------------------------
unsigned long long atoll (const char *s)
{
    unsigned long long val = 0;

    for (unsigned i = 0; i < strlen (s); i++)
        val = 10*val + s [i] - '0';

    return val;
}

// -----------------------------------------------------------------------------
const char* llToStr ( unsigned long long val )
{
    static char s [40];

    memset (s, 0, sizeof(s));

    for (int i = 0; 0 < val; i++, val /= 10)
        s [i] = '0' + (val % 10) ;

    int n = strlen (s);
    for (int i = 0; i < n/2; i++)  {
        char  c   = s [i];
        s [i]     = s [n-i-1];
        s [n-i-1] = c; 
    }
    return s;
}

// -----------------------------------------------------------------------------
void jolopez (
    const char *s )
{
    unsigned long long  longLongVal = atoll (s);

    Serial.print   ("jolopez: ");
    Serial.println (llToStr (longLongVal));
}

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (115200);

#if 0
    char s [40];
    sprintf (s, "%s: %s", __func__, llToStr (1923123123123));
    Serial.println (s);
#else
    jolopez ("1923123123123");
    jolopez ("1900000000003");
    jolopez ("1");
    jolopez ("12");
    jolopez ("123");
#endif
}

// -----------------------------------------------------------------------------
void
loop (void)
{
}

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.

Thank you all.

Try

unsigned long long val = 0;
PriUint64<DEC>(val);
Tst:61: error: 'PriUint64' was not declared in this scope

     PriUint64<DEC>(val);

How to Print uint64_t in Arduino suggests there's something to this.

i try following as well (same error)

    Serial.println (PriUint64<DEC>(val));

This works for me to print long long values.

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() {}