Deep understanding of the use of atoi () and itoa () functions _c language

Source: Internet
Author: User
The prototype of the ITOA () function is: char *itoa (int value, char *string,int radix);
The itoa () function has 3 parameters: The first argument is the number to convert, the second parameter is the target string to write the transformation result, and the third parameter is the cardinality used to convert the number. In the example, the conversion base is 10. 10: decimal; 2: Binary ...
Itoa is not a standard C function, it is Windows specific, if you want to write Cross-platform program, please use sprintf.
is extended under the Windows platform, with sprintf in the standard library, more powerful than this, with a similar usage to printf:
Char str[255];
sprintf (str, "%x", 100); A string that converts 100 to a 16-binary representation.
The following is a decimal octal method:
Copy Code code as follows:

#include "stdio.h"
#include "Stdlib.h"
int main (void)
{
int num = 10;
Char str[100];
ITOA (num, str, 8); Converts integer 10 to octal to save in a str character array
printf ("%s\n", str);
System ("pause");
return 0;
}

The following is a decimal conversion method:
Copy Code code as follows:

#include "stdio.h"
#include "Stdlib.h"
int main (void)
{
int num = 15;
Char str[100];
int n = atoi (itoa (num, str, 2)); Converts num to a binary string, then converts the string to an integer
printf ("%d\n", N);
System ("pause");
return 0;
}

Extension of the itoa () function:
Copy Code code as follows:

char *_itoa (int value, char *string, int radix);
Char *_i64toa (__int64 value, char *string, int radix);
char * _UI64TOA (unsigned _int64 value, char *string, int radix);
wchar_t * _itow (int value, wchar_t *string, int radix);
wchar_t * _i64tow (__int64 value, wchar_t *string, int radix);
wchar_t * _ui64tow (unsigned __int64 value, wchar_t *string, int radix);

The program code is as follows:
Copy Code code as follows:

#include "stdio.h"
#include "Stdlib.h"
int main (void)
{
Char buffer[20];
int i = 3445;
Long L = -344115l;
unsigned long ul = 1234567890UL;
_itoa (i, buffer, 10);
printf ("String of Integer%d (radix):%s\n", I, buffer);
_itoa (i, buffer, 16);
printf ("String of Integer%d (radix): 0x%s\n", I, buffer);
_itoa (i, buffer, 2);
printf ("String of Integer%d (radix 2):%s\n", I, buffer);
_ltoa (l, buffer, 16);
printf ("String of long int%ld (radix): 0x%s\n", L,buffer);
_ULTOA (UL, buffer, 16);
printf ("String of unsigned Long%lu (radix): 0x%s\n", Ul,buffer);
System ("pause");
return 0;
}

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.