Questions tagged [strtol]

strtol() is the C runtime library function for converting the text representation of a number to a long integer. This SO tag also applies to strtoll(), strtoul(), and strtoull() which perform the same conversion to types "long long", "unsigned long", and "unsigned long long".

Use this tag on for all questions about the use of the strtol() family of functions or where such a function does not seem to be working correctly.

Closely related are:

  • for converting text to int (includes atol(), atoll(), and atoq())
  • atol() for long
  • for double
  • for double with unambiguous error indication
  • for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

              long int strtol   (const char *nptr, char **endptr, int base);
         long long int strtoll  (const char *nptr, char **endptr, int base);
     unsigned long int strtoul  (const char *nptr, char **endptr, int base);
unsigned long long int strtoull (const char *nptr, char **endptr, int base);

BSD also has

                quad_t strtoq   (const char *nptr, char **endptr, int base);
              u_quad_t strtouq  (const char *nptr, char **endptr, int base);

strtol() converts from text in any base from 2 through 36. Or it can choose a base automatically (if base is specified as zero) same as the C compiler does depending on how the number is written: a leading 0 chooses octal, a leading 0x or 0X chooses hexadecimal, else decimal. Leading whitespace is skipped.

If endptr is not NULL, the pointer is set to the next character not converted. A conversion error is indicated by errno set to EINVAL (but set errno to zero before calling).

strtol() returns the result of the conversion, unless the value would underflow or overflow. It returns LONG_MIN if an underflow occurs, and LONG_MAX in case of an overflow. In both cases, errno is set to ERANGE. Similar is the case for strtoll() (LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX respectively).

164 questions
68
votes
7 answers

atol() v/s. strtol()

What is the difference between atol() & strtol()? According to their man pages, they seem to have the same effect as well as matching arguments: long atol(const char *nptr); long int strtol(const char *nptr, char **endptr, int base); In a…
user191776
27
votes
16 answers

Convert a hexadecimal string to an integer efficiently in C?

In C, what is the most efficient way to convert a string of hex digits into a binary unsigned int or unsigned long? For example, if I have 0xFFFFFFFE, I want an int with the base10 value 4294967294.
M
10
votes
5 answers

std::atoll with VC++

I have been using std::atoll from cstdlib to convert a string to an int64_t with gcc. That function does not seem to be available on the Windows toolchain (using Visual Studio Express 2010). What is the best alternative? I am also interested in…
Cookie
  • 10,754
  • 13
  • 47
  • 69
10
votes
6 answers

Getting a hexadecimal number into a program via the command line

I can do this: int main(int argc, char** argv) { unsigned char cTest = 0xff; return 0; } But what's the right way to get a hexadecimal number into the program via the command line? unsigned char cTest = argv[1]; doesn't do the trick. That…
Pieter
  • 28,337
  • 74
  • 160
  • 235
8
votes
6 answers

C - Comparing numeric strings

Out of professional curiosity, what is the safest / fastest / most efficient way to compare two fully numeric strings in C? #include #include #include int main(void){ char str1[5] = "123"; char str2[5] = "123"; char…
Valdogg21
  • 1,024
  • 4
  • 11
  • 23
8
votes
1 answer

the base argument of std::stoi

The stoi function of c++ is defined as: int stoi(const std::string& str, std::size_t* pos = 0, int base = 10); as you can see, the base argument is defaulted as 10, so by default it could only handle decimal numbers. By setting base to 0, it could…
fluter
  • 11,341
  • 7
  • 44
  • 77
8
votes
1 answer

Output of strtoull() loses precision when converted to double and then back to uint64_t

Consider the following: #include #include int main() { std::cout << std::hex << "0x" << std::strtoull("0xFFFFFFFFFFFFFFFF",0,16) << std::endl << "0x" << uint64_t(double(std::strtoull("0xFFFFFFFFFFFFFFFF",0,16)))…
Adama
  • 700
  • 5
  • 21
8
votes
2 answers

Why can't you just check if errno is equal to ERANGE?

I've been trying to properly convert a char array to a long with strtol, check if there was an overflow or underflow and then do an int cast on the long. Along the way, I've noticed a lot of code that looks like this if ((result == LONG_MAX ||…
Luis Averhoff
  • 557
  • 2
  • 7
  • 21
8
votes
4 answers

Basics of strtol?

I am really confused. I have to be missing something rather simple but nothing I am reading about strtol() is making sense. Can someone spell it out for me in a really basic way, as well as give an example for how I might get something like the…
James Thompson
  • 225
  • 1
  • 5
  • 13
7
votes
3 answers

What is the difference between strtol and strtoul?

I met some unexcepted result of strtol in c Here is the sample program. #include #include #include int main() { printf("%x\n", strtol("0xfffff70A", NULL, 0)); return 0; } and the output of this simple…
user1668903
  • 367
  • 3
  • 11
6
votes
4 answers

Confusing language in specification of strtol, et al

The specification for strtol conceptually divides the input string into "initial whitespace", a "subject sequence", and a "final string", and defines the "subject sequence" as: the longest initial subsequence of the input string, starting with the…
R.. GitHub STOP HELPING ICE
  • 195,354
  • 31
  • 331
  • 669
6
votes
1 answer

Aliased arguments in strtol

Here is how strtol has to be declared according to § 7.22.1.4 from C11 (n1570): #include long int strtol (const char *restrict nptr, char **restrict endptr, int base); As far as I know, the restrict…
md5
  • 22,211
  • 2
  • 40
  • 91
6
votes
3 answers

strtol reusing param

This code seems to work as expected, populates an array of numbers using a single pointer #include #include #include int main(void) { int arr[4], count = 0, i; char *p, s[32] = " \t 10, 15 \n ,20, 25 ,…
David Ranieri
  • 36,077
  • 6
  • 44
  • 85
5
votes
3 answers

Convert long integer(decimal) to base 36 string (strtol inverted function in C)

I can use the strtol function for turning a base36 based value (saved as a string) into a long int: long int val = strtol("ABCZX123", 0, 36); Is there a standard function that allows the inversion of this? That is, to convert a long int val…
Łukasz Przeniosło
  • 2,285
  • 3
  • 26
  • 56
4
votes
5 answers

Do I need to cast the result of strtol to int?

The following code does not give a warning with g++ 4.1.1 and -Wall. int octalStrToInt(const std::string& s) { return strtol(s.c_str(), 0, 8); } I was expecting a warning because strtol returns a long int but my function is only returning a…
Michael Kristofik
  • 31,476
  • 15
  • 72
  • 121
1
2 3
10 11