C language Itoa () function and atoi () function (integer to character C implementation)

Source: Internet
Author: User
Tags sprintf

C language Itoa () function and atoi () function (integer to character C implementation)

The C language provides several standard library functions that convert numbers of any type (integer, long, float, and so on) to a string.

1.int/float to String/array:
The C language provides several standard library functions that can convert numbers of any type (integer, long, float, and so on) to a string, and the methods and descriptions of each function are listed below.
Itoa (): Converts an integer value to a string.
Ltoa (): Converts a long integer value to a string.
Ultoa (): Converts an unsigned long integer value to a string.
GCVT (): Converts a floating-point number to a string, rounded.
ECVT (): Converts a double-precision floating-point value to a string that does not contain a decimal point in the conversion result.
FCVT (): Specifies the number of digits for the conversion precision, and the remainder with ECVT ().

In addition, you can use the SPRINTF series function to convert a number to a string, which runs slower than the itoa () series function

2. String/array to Int/float
The C + + language provides several standard library functions that can convert strings to any type (integer, long, float, and so on).
Atof (): Converts a string to a double-precision floating-point value.
Atoi (): Converts a string to an integer value.
ATOL (): Converts a string to a long integer value.
Strtod (): Converts a string to a double-precision floating-point value and reports all remaining digits that cannot be converted.
Strtol (): Converts a string to a long integer value and reports all remaining digits that cannot be converted.
Strtoul (): Converts a string to an unsigned long value and reports any remaining digits that cannot be converted.

The following is an example of converting an integer to a string using the Itoa () function:
# include <stdio.h>
# include <stdlib.h>
void Main (void)
{
int num = 100;
Char str[25];
ITOA (num, str, 10);
printf ("The number ' num ' is%d and the string ' str ' is%s. \ n", num, str);
}

The itoa () function has 3 parameters: The first argument is the number to be converted, the second argument is the target string to write the result of the conversion, and the third argument is the cardinality used to transfer the number. In the example above, the conversion cardinality is 10. 10: decimal; 2: Binary ...

Itoa is not a standard C function, it is unique to Windows, if you want to write a cross-platform program, please use sprintf. is expanded under the Windows platform, the standard library has sprintf, the function is stronger than this, the usage is similar to printf:

Char str[255];
sprintf (str, "%x", 100); A string that converts 100 to a 16 binary representation.

The following functions can convert integers to strings:
----------------------------------------------------------
Function name with
----------------------------------------------------------
Itoa () converts an integer value to a string
Itoa () Converts a long integer value to a string
Ultoa () converts an unsigned long value to a string

One, atoi ()--Converts a string to an integer
Test Center: An understanding of the associated ASCII code when a string is converted to a number.

C Implementation:
#include <ctype.h>
#include <stdio.h>
int atoi (char s[]);
int main (void)
{
char s[100];
Gets (s);
printf ("integer=%d\n", Atoi (s));
return 0;
}
Int atoi (char s[])
{
int i,n,sign;
For (I=0;isspace (S[i]); i++)//skip whitespace characters;
sign= (s[i]== '-')? -1:1;
if (s[i]== ' + ' | | s[i]== '-')//skip symbol
i++;
For (N=0;isdigit (S[i]); i++)
n=10*n+ (s[i]-' 0 ');//convert numeric characters to shaping numbers
return sign *n;
}


C + + implementation:
1 #include <iostream>
2 using namespace std;
3
4 int str2int (const char *STR)
5 {
6 int temp = 0;
7 const char *ptr = str; PTR saves the str string beginning with
8
9 if (*str = = '-' | | *str = = ' + ')///If the first character is a sign,
10//moves to the next character
one str++;
[*str! = 0]
{
if (*str < ' 0 ') | | (*str > ' 9 ')) If the current character is not a number
16 {//Then exit the loop
break;

Temp = temp * + (*STR-' 0 ');//If the current character is a number, calculate the value
str++;//move to the next character
+}
if (*ptr = = '-')//if the character The string is preceded by "-" and converted to its opposite number
(
temp =-temp;
)

to temp;
()

Int main ()
{
+ int n = 0;
P[10 char] = "";
Cin.getline
(P, 20);//Gets a string from the terminal
N = str2int (P);//convert string to Integer
PNs
cout << n << en dl
up to
0;
A.}

Second, itoa ()--Converts an integer to a string
By converting the number of integers to "0" to the char type and into the character array. Note, however, that you need to use a string reversal method

C Language implementation:
#include <ctype.h>
#include <stdio.h>
void itoa (int n,char s[]);
Atoi function: Converting s to shaping number
int main (void)
{
int n;
Char s[100];
printf ("Input n:\n");
scanf ("%d", &n);
printf ("The string: \ n");
Itoa (n,s);
return 0;
}
void itoa (int n,char s[])
{
int i,j,sign;
if ((sign=n) <0)//record symbol
n=-n;//make n a positive number
i=0;
do{
s[i++]=n%10+ ' 0 ';//Remove a number
}
while ((n/=10) >0);//Delete the number
if (sign<0)
s[i++]= '-';
s[i]= ' + ';
for (j=i;j>=0;j--)//generated number is reverse order, so to reverse the output
printf ("%c", S[j]);
}

is a function of int to string type

C + + implementation:
1 #include <iostream>
2 using namespace Std;
3
4 void Int2str (int n, char *str)
5 {
6 char buf[10] = "";
7 int i = 0;
8 int len = 0;
9 int temp = N < 0? -n:n; Temp is the absolute value of n
10
if (str = = NULL)
12 {
return;
14}
(temp)
16 {
buf[i++] = (temp% 10) + ' 0 '; Deposit each of the numbers on the temp buf
temp = TEMP/10;
19}
20
Len = n < 0? ++I:I; If n is negative, you need one more to store the minus sign
Str[i] = 0; At the end is Terminator 0
while (1)
24 {
i--;
if (Buf[len-i-1] ==0)
27 {
break;
29}
Str[i] = buf[len-i-1]; Copy the characters in the BUF array to a string
31}
if (i = = 0)
33 {
Str[i] = '-'; If it is negative, add a minus sign
35}
36}
37
int main ()
39 {
Nnum int;
(p[10 char);
42
cout << "Please input an integer:";
Cin >> Nnum;
cout << "Output:";
Int2str (Nnum, p); Integral type converted to string
cout<< p << Endl;
48
return 0;
50}

C language Itoa () function and atoi () function (integer to character C implementation)

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.