The usage of strtol of function and strtoul of function in C language is analyzed

  • 2020-04-02 03:17:37
  • OfStack

C strtol() function: converts a string to long
The header file:


#include <stdlib.h>

The strtol() function is used to convert a string to a long integer (long). The prototype is:


long int strtol (const char* str, char** endptr, int base);

STR is the string to be converted, endstr is the pointer to the first character that cannot be converted, and base is the base of the string STR.

Strtol () converts the parameter STR string into an integer number (long) based on the parameter base. The parameters base range from 2 to 36, or 0. The parameter base represents the hexadecimal method adopted by STR. If the base value is 10, the hexadecimal system will be adopted; if the base value is 16, the hexadecimal system will be adopted.

Strtol () will scan the STR string, skip the previous whitespace characters (such as space, TAB indentation, etc., can be detected by the isspace() function), do not start the conversion until a number or a plus or minus sign is encountered, and when a non-number or string end ('\0') to end the conversion, and return the result.

Two notes:

Base is converted by default when the value of base is 0, but hexadecimal conversion is used if the '0x'/'0x' preposition character is encountered, and hexadecimal conversion is used if the '0' preposition character is encountered. If the endptr is not NULL, the unqualified character pointer will be returned by endptr. If the endptr is NULL, the parameter is invalid or not used.

[return value] returns the converted long integer; Returns 0(0L) if it cannot be converted or if STR is an empty string; If the conversion yields a value beyond the range long int can represent, the function returns LONG_MAX or LONG_MIN (defined in the limits. H header file) and sets the value of errno to ERANGE.

Converts a string to base 10.


#include <stdio.h>
#include <stdlib.h>
int main ()
{
  char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
  char * pEnd;
  long int li1, li2, li3, li4;
  li1 = strtol (szNumbers,&pEnd,10);
  li2 = strtol (pEnd,&pEnd,16);
  li3 = strtol (pEnd,&pEnd,2);
  li4 = strtol (pEnd,NULL,0);
  printf (" Converted to 10 Into the system : %ld , %ld , %ld , %ldn", li1, li2, li3, li4);
  system("pause");
  return 0;
}

Execution results:
Convert to base 10:


2001 , 6340800 , -3624224 , 7340031

C strtoul() function: converts a string to an unsigned long integer
The header file:


#include <stdlib.h>

The strtoul() function is derived from "string to unsigned long" and is used to convert strings to unsigned long integers.


  unsigned long strtoul (const char* str, char** endptr, int base);

STR is the string to be converted, endstr is the pointer to the first character that cannot be converted, and base is the base of the string STR.

Strtoul () converts the parameter STR string into an unsigned long integer based on the parameter base. The parameters base range from 2 to 36, or 0. The parameter base represents the hexadecimal method adopted by STR. If the base value is 10, the hexadecimal system will be adopted; if the base value is 16, the hexadecimal number will be adopted.

Strtoul () scans the STR string, skips the preceding whitespace characters (such as Spaces, TAB indentation, etc., can be detected by the isspace() function), and does not begin the conversion until a number or sign is encountered, and then ends the conversion when a non-number or string ends ('\0'), and returns the result.

Two notes:

Base is converted by default when the value of base is 0, but hexadecimal conversion is used if the '0x'/'0x' preposition character is encountered, and hexadecimal conversion is used if the '0' preposition character is encountered. If the endptr is not NULL, the unqualified character pointer will be returned by endptr. If the endptr is NULL, the parameter is invalid or not used. The example at the end of this article does not show the use of the endptr parameter. You can refer to the example of the strtol() function for a more intuitive understanding of the endptr parameter.

[return value] returns the converted unsigned long integer; Returns 0 if it cannot be converted or if STR is an empty string; If the resulting value is beyond the range that unsigned long int can represent, the function returns ULONG_MAX (defined in the limits. H header) and sets the value of errno to ERANGE.

Tips: ANSI C specification defines stof(), atoi(), atol(), strtod(), strtol(), strtoul(), a total of 6 functions that can convert strings into Numbers, you can learn by comparison. In addition, there are 5 new functions in C99 / C++11, which are atoll(), strtof(), strtold(), strtoll(), and strtoull().

Example: converts the input string to an unsigned long integer.


#include <stdio.h>
#include <stdlib.h>
int main ()
{
  char buffer [256];
  unsigned long ul;
  printf ("Enter an unsigned number: ");
  fgets (buffer, 256, stdin);
  ul = strtoul (buffer, NULL, 0);
  printf ("Value entered: %lu.n", ul);
  system("pause");
  return 0;
}

Operation results:


Enter an unsigned number: 017cyuyan
Value entered: 15.

Since the base parameter is set to 0 and the string "017cyuyan" begins with "0", the hexadecimal conversion is used.


Related articles: