Is there a strtol equivalent that does not require a null-terminated string?

strntol is probably what you're after... it's not standard C, though.


No such function in the standard library. You will either have to use the temporary buffer method, or write your own function from scratch.


If you're that pressed for efficiency, you can probably motivate the time to write and debug your own.

But: just do it with a copy; you probably have an upper bound for how long the string can be (a decimal numeral that fits in a long has a strict upper bound on its maximum length), so you can have a static buffer. Then profile your entire application, and see if the copying/conversion really is a bottleneck. If it really is, then you know you need to write your own.

Here's a rough (untested, browser-written) starting point:

long limited_strtol(const char *string, size_t len)
{
  long sign = 1;
  long value = 0;

  for(; len > 0 && *string == '-'; string++, len--)
    sign *= -1;

  for(; len > 0 && isdigit(*string); string++, len--)
  {
   value *= 10;
   value += *string - '0';
   len--;
   string++;
  }
  return sign * value;
}

To answer your question: no, there is no standard function, but it is simple enough to write your own:

#include <stdio.h>
#include <ctype.h>

int natoi(char *s, int n)
{
    int x = 0;
    while(isdigit(s[0]) && n--)
    {
        x = x * 10 + (s[0] - '0');      
        s++;
    }
    return x;
}

int main(int argc, char*argv[])
{
    int i;
    for(i = 1; i < argc; i++)
        printf("%d: %d\n", i, natoi(argv[i], 5));
}

Tags:

C

String

Std