In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use the string conversion function of C language", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this article "how to use the string conversion function of C language".
String to integer
There are two functions that convert strings to integers, and their function prototypes are as follows:
Int _ cdecl atoi (const char * _ Str); long _ cdecl atol (const char * _ Str)
The use of both functions is simple: the atoi function converts a string to an integer int, and the atol function converts a string to a long shaping long int. If the conversion is invalid and the return value is 0, let's take a look at the use of these two functions through a simple example.
# include # include int main (int argc, char** argv) {int val; long val_l; char str [20]; strcpy (str, "1234"); val = atoi (str); printf ("string value =% s, integer value =% d\ n", str, val); strcpy (str, "abc"); val = atoi (str); printf ("string value =% s, integer value =% d\ n", str, val) Strcpy (str, "98993489"); val_l = atol (str); printf ("\ nstring value =% s, long integer value =% ld\ n", str, val_l); strcpy (str, "abc123"); val_l = atol (str); printf ("string value =% s, long integer value =% ld\ n", str, val_l); return 0;}
Assign different values to the string, and then convert the string using the atoi function and the atol function. The output is as follows:
As can be seen from the results, only when the string is valid can it be correctly converted to an integer, otherwise the result of the conversion will be 0.
There are two other functions that convert a string to an integer:
Long _ cdecl strtol (const char * _ restrict__ _ Str,char * * _ restrict__ _ EndPtr,int _ Radix); unsigned long _ cdecl strtoul (const char * _ restrict__ _ Str,char * * _ restrict__ _ EndPtr,int _ Radix)
The strtol () function, which converts a string to a long integer (long), takes three parameters:
_ Str is the string to be converted
_ EndPtr is the pointer to the first unconvertible character
_ Radix is the base used by the string str.
The trtol () function converts the parameter str string to the growing integer (long) based on the parameter base. The parameter base ranges from 2 to 36, or 0. The parameter base represents the binary mode used by str. For example, the base value is 10, the base value is 16, and the hexadecimal system is used.
Here is a simple example to demonstrate the use of the strtol () function.
# include # include int main (int argc, char** argv) {char str [30] = "123456 abc"; char* pEnd; long ret1,ret2; ret1 = strtol (str, & pEnd, 10); ret2 = strtol (pEnd, & pEnd, 10); printf ("number 1 is:% ld\ n", ret1); printf ("number 2 is:% ld\ n", ret2) Printf ("string part is:% s\ n", pEnd); return 0;}
Define a string, and then use the strtol () function to convert the numbers in the string. The output is as follows:
As you can see from the printed result, the strtol () function successfully converts both numbers in the string into integers.
When the strtol () function reads the string, the first space is returned because the white space character cannot be converted, so the function returns and stores the white space character in pEnd, and then continues the conversion from the blank position. After converting the string "- 456" into a number, it encounters a blank character and cannot continue the conversion. The function exits and stores the second blank character in pEnd. Finally, change the remaining strings and print them out.
Through the strtol () function, you can intelligently extract the numbers of a string.
The strtoul function is used to convert strings to unsigned long integers (unsigned long) in much the same way as the strtol () function.
Modify the above code as follows:
# include # include int main (int argc, char** argv) {char str [30] = "123,456 abc"; char* pEnd; long ret1,ret2; ret1 = strtoul (str, & pEnd, 10); ret2 = strtoul (pEnd, & pEnd, 10); printf ("number 1 is:% lu\ n", ret1); printf ("number 2 is:% lu\ n", ret2) Printf ("string part is:% s\ n", pEnd); return 0;}
The output is as follows:
If you precede the number of a string with a negative sign, the result of the conversion will be an error.
There are two things to note when using the strtol () function and the strtoul function:
When the value of base is 0, decimal conversion is used by default, but hexadecimal conversion is used if the '0x' /' 0X' leading character is encountered, and octal conversion is used if the'0' leading character is encountered.
If endptr is not NULL, the character pointer that terminates if it does not meet the condition will be returned by endptr; if endptr is NULL, the parameter is invalid or not used.
The above is about the content of this article on "how to use the string conversion function of C language". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.