# Convert with atol() string to long int after strtok

**URL:** <https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322>\
**Category:** Programming\
**Created:** [June 12, 2020, 12:21pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322 "2020-06-12T12:21:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jolopez](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jolopez](https://forum.arduino.cc/u/jolopez)\
**Post date:** [June 12, 2020, 12:21pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/1 "2020-06-12T12:21:05Z")

</div>

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:

```cpp
    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.

![](https://ibb.co/NYRkGk4)

Any idea about whats happening?

Thank you all.

---

<div class="post-metadata">

**Author:** ![pcbbc](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@pcbbc](https://forum.arduino.cc/u/pcbbc)\
**Post date:** [June 12, 2020, 12:32pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/2 "2020-06-12T12:32:13Z")

</div>

What's this all about?

```cpp
PriUint64<DEC>

```

What was wrong with...

```cpp
Serial.println(longintvalue);

```

---

<div class="post-metadata">

**Author:** ![jolopez](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jolopez](https://forum.arduino.cc/u/jolopez)\
**Post date:** [June 12, 2020, 1:12pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/3 "2020-06-12T13:12:43Z")

</div>

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:

> **[2020-06-12-15h11-42 hosted at ImgBB](https://ibb.co/VVBPnmK)**
>
> Image 2020-06-12-15h11-42 hosted in ImgBB

Thanks for the reply!

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [June 12, 2020, 1:23pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/4 "2020-06-12T13:23:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1105255_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [June 12, 2020, 1:33pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/5 "2020-06-12T13:33:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [June 12, 2020, 1:48pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/6 "2020-06-12T13:48:16Z")

</div>

consider (if desperate)

results

```cpp
jolopez: 1923123123123
jolopez: 1900000000003
jolopez: 1
jolopez: 12
jolopez: 123

```

```cpp
// -----------------------------------------------------------------------------
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)
{
}

```

---

<div class="post-metadata">

**Author:** ![jolopez](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jolopez](https://forum.arduino.cc/u/jolopez)\
**Post date:** [June 12, 2020, 2:33pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/7 "2020-06-12T14:33:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [June 12, 2020, 2:47pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/8 "2020-06-12T14:47:03Z")

</div>

Try

```cpp
unsigned long long val = 0;
PriUint64<DEC>(val);

```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [June 12, 2020, 5:24pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/9 "2020-06-12T17:24:16Z")

</div>

```cpp
Tst:61: error: 'PriUint64' was not declared in this scope

    PriUint64<DEC>(val);

```

[How to Print uint64\_t in Arduino](https://yoursunny.com/t/2018/PriUint64/) suggests there's something to this.

i try following as well (same error)

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

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [June 12, 2020, 6:39pm UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/10 "2020-06-12T18:39:23Z")

</div>

This works for me to print long long values.

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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:35am UTC](https://forum.arduino.cc/t/convert-with-atol-string-to-long-int-after-strtok/660322/11 "2021-05-06T00:35:31Z")

</div>


