Non-decimal radices/Convert: Difference between revisions

Content added Content deleted
Line 232: Line 232:
<lang c>#include <stdlib.h>
<lang c>#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <assert.h>


#define MAX_OUTSTR_LEN 65
#define MAX_OUTSTR_LEN 65
Line 260: Line 261:
long from_base(const char *num_str, int base)
long from_base(const char *num_str, int base)
{
{
char *endptr;
return strtol(num_str, NULL, base);
int result = strtol(num_str, &endptr, base);
// there is also strtoul() for parsing into an unsigned long
assert(*endptr == '\0'); /* if there are any characters left, then string contained invalid characters */
// in C99, there is also strtoll() and strtoull() for parsing into long long and unsigned long long, respectively
return result;
/* there is also strtoul() for parsing into an unsigned long */
/* in C99, there is also strtoll() and strtoull() for parsing into long long and unsigned long long, respectively */
}</lang>
}</lang>