On the usage of strtol () function and Strtoul () function in C language _c language

Source: Internet
Author: User
Tags numeric

C language Strtol () function: Converts a string to long (Long integer)
header file:

#include <stdlib.h>

The Strtol () function is used to convert a string to a long integer, which is a prototype:

Long int strtol (const char* STR, char** endptr, int base);

"Parameter description" STR is the string to be converted, endstr as a pointer to the first character that cannot be converted, and base is the system used by the string str.

The function Description Strtol () converts the parameter str string to the number of growth integers (long) based on the parameter base. Parameter base range from 2 to 36, or 0. The parameter base represents the way in which STR is used, such as the base value of 10, 10, or 16 if the base value is 16.

Strtol () scans the parameter str string, skipping the preceding whitespace characters (for example, spaces, tab indents, etc.) can be detected by the isspace () function until a number or positive sign is started to do the conversion, and then a non-numeric or end of the string ('. ') ends the conversion and returns the result.

Two points Note:

    1. When the value of base is 0 o'clock, the default is 10 conversion, but if the ' 0x '/' 0X ' front character is encountered, the 16 conversion is used, and the ' 0 ' front character is encountered with a 8 conversion.
    2. If the endptr is not NULL, the character pointer that is encountered that does not meet the criteria is passed back to the endptr, and if endptr is null, the argument is invalid, or the parameter is not used.

Return value returns the number of long integers converted, or 0 (0L) if it cannot be converted or if Str is an empty string, or if the converted value is more than the range that a long int can represent, the function returns LONG_MAX or long_min (defined in the Limits.h header file), and the The value of the errno is set to Erange.

The example converts the string to 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 ("Convert to 10 binary:%ld,%ld,%ld,%ld\n", Li1, Li2, Li3, li4);
  System ("pause");
  return 0;
}

Execution results:
Convert to 10:

2001, 6340800,-3624224, 7340031

C language Strtoul () function: Converts a string to a unsigned long (unsigned length integer)
header file:

#include <stdlib.h>

The Strtoul () function is derived from "string to unsigned Long", which is used to convert a string to an unsigned long integer (unsigned long), which is a prototype:

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

"Parameter description" STR is the string to be converted, endstr as a pointer to the first character that cannot be converted, and base is the system used by the string str.

The function Description Strtoul () converts the parameter str string to an unsigned long integer (unsigned long) based on the parameter base. Parameter base range from 2 to 36, or 0. The parameter base represents the way in which STR is used, such as a base value of 10, 10, or 16 in the base value of 16.

Strtoul () scans the parameter str string, skipping the preceding whitespace characters (such as spaces, tab indents, etc.) can be detected by the isspace () function until a number or positive sign is started to do the conversion, and then a non-numeric or end of the string ('? ') is converted. and returns the result.

Two points Note:

    1. When the value of base is 0 o'clock, the default is 10 conversion, but if the ' 0x '/' 0X ' front character is encountered, the 16 conversion is used, and the ' 0 ' front character is encountered with a 8 conversion.
    2. If the endptr is not NULL, the character pointer that is encountered that does not meet the criteria is passed back to the endptr, and if endptr is null, the argument is invalid, or the parameter is not used. The final example of this article does not show the use of the Endptr parameter, you can refer to the example of the Strtol () function to see the endptr parameters more intuitively.

return value returns the converted unsigned long integer, if it cannot be converted or STR is an empty string, returns 0, and if the converted value exceeds the range that unsigned long int can represent, the function returns ULONG_MAX (defined in the Limits.h header file) and the The value of the errno is set to Erange.

Warm tip: ANSI C specification defines the Stof (), Atoi (), Atol (), strtod (), Strtol (), Strtoul () a total of 6 functions that can convert strings to numbers, and we can compare learning. In addition, there are 5 additional functions in the C99/C++11 specification, namely, Atoll (), Strtof (), Strtold (), Strtoll (), Strtoull (), which do not introduce, please learn by yourselves.

Example: Converts an 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;
}

Run Result:

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

Because the base parameter is set to 0 and the string "017cyuyan" begins with "0", a 8-binary conversion is used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.