1. 程式人生 > >C語言itoa()函數和atoi()函數詳解(整數轉字符C實現)

C語言itoa()函數和atoi()函數詳解(整數轉字符C實現)

獲取 c++語言 end atof 定位 ray iostream 寫入 blog

C語言提供了幾個標準庫函數,可以將任意類型(整型、長整型、浮點型等)的數字轉換為字符串。

1.int/float to string/array:

C語言提供了幾個標準庫函數,可以將任意類型(整型、長整型、浮點型等)的數字轉換為字符串,下面列舉了各函數的方法及其說明。
● itoa():將整型值轉換為字符串。
● ltoa():將長整型值轉換為字符串。
● ultoa():將無符號長整型值轉換為字符串。
● gcvt():將浮點型數轉換為字符串,取四舍五入。
● ecvt():將雙精度浮點型值轉換為字符串,轉換結果中不包含十進制小數點。
● fcvt():指定位數為轉換精度,其余同ecvt()。
除此外,還可以使用sprintf
系列函數把數字轉換成字符串,其比itoa()系列函數運行速度慢

2. string/array to int/float
C/C++語言提供了幾個標準庫函數,可以將字符串轉換為任意類型(整型、長整型、浮點型等)。 ● atof():將字符串轉換為雙精度浮點型值。
● atoi():將字符串轉換為整型值。
● atol():將字符串轉換為長整型值。
● strtod():將字符串轉換為雙精度浮點型值,並報告不能被轉換的所有剩余數字。
● strtol():將字符串轉換為長整值,並報告不能被轉換的所有剩余數字。
● strtoul():將字符串轉換為無符號長整型值,並報告不能被轉換的所有剩余數字。

以下是用itoa()函數將整數轉換為字符串的一個例子:
# 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);
}

itoa()函數有3個參數:第一個參數是要轉換的數字,第二個參數是要寫入轉換結果的目標字符串,第三個參數是轉移數字時所用 的基數。在上例中,轉換基數為10。10:十進制;2:二進制...

itoa並不是一個標準的C函數,它是Windows特有的,如果要寫跨平臺的程序,請用sprintf。是Windows平臺下擴展的,標準庫中有sprintf,功能比這個更強,用法跟printf類似:

char str[255];
sprintf(str, "%x", 100); //將100轉為16進制表示的字符串。

下列函數可以將整數轉換為字符串:
----------------------------------------------------------
函數名 用
----------------------------------------------------------
itoa() 將整型值轉換為字符串
itoa() 將長整型值轉換為字符串
ultoa() 將無符號長整型值轉換為字符串
一、atoi()——把字符串轉換成整型數
考點:字符串轉換為數字時,對相關ASCII碼的理解。 C實現:
 1 #include <ctype.h>
 2 #include <stdio.h>
 3 int atoi (char s[]);
 4 
 5 int main(void){
 6     char s[100];
 7     gets(s);
 8     printf("integer=%d\n",atoi(s));
 9     return 0;
10 }
11 
12 int atoi (char s[]){
13     int i,n,sign;
14     for(i=0;isspace(s[i]);i++)//
跳過空白符; 15 sign=(s[i]==-)?-1:1; 16 if(s[i]==+||s[i]== -)//跳過符號 17 i++; 18 for(n=0;isdigit(s[i]);i++) 19 n=10*n+(s[i]-0);//將數字字符轉換成整形數字 20 return sign *n; 21 }

C++實現:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int str2int(const char *str){
 5     int temp = 0;
 6     const char *ptr = str;  //ptr保存str字符串開頭
 7     if (*str == - || *str == +){//如果第一個字符是正負號,
 8     str++;                      //則移到下一個字符
 9     }
10     while(*str != 0){
11         if ((*str < 0) || (*str > 9)){//如果當前字符不是數字
12             break;                       //則退出循環
13             }
14         temp = temp * 10 + (*str - 0); //如果當前字符是數字則計算數值
15         str++;      //移到下一個字符
16          }
17     if (*ptr == -){                   //如果字符串是以“-”開頭,則轉換成其相反數
18         temp = -temp;
19     }
20     return temp;
21 }
22 
23 int main(void){
24     int n = 0;  
25     char p[10] = "";
26     cin.getline(p, 20);   //從終端獲取一個字符串
27     n = str2int(p);      //把字符串轉換成整型數
28     cout << n << endl;
29     return 0;
30 }

二、itoa()——把一整數轉換為字符串

通過把整數的各位上的數字加“0”轉換成char類型並存到字符數組中。但是要註意,需要采用字符串逆序的方法 C語言實現:
 1 #include <ctype.h>
 2 #include <stdio.h>
 3 
 4 
 5 void itoa (int n,char s[]){
 6     int i,j,sign;
 7     if((sign=n)<0)//記錄符號
 8         n=-n;//使n成為正數
 9     i=0;
10     do{
11         s[i++]=n%10+0;//取下一個數字
12     }while ((n/=10)>0);//刪除該數字
13     if(sign<0)s[i++]=-;
14     s[i]=\0;
15     for(j=i;j>=0;j--)//生成的數字是逆序的,所以要逆序輸出
16         printf("%c",s[j]);
17 }
18 
19 
20 //atoi 函數:將s轉換為整形數
21 int main(void){
22     int n;
23     char s[100];
24     printf("Input n:\n");
25     scanf("%d",&n);
26     printf("the string : \n");
27     itoa(n,s);
28     return 0;
29 }

是int 轉string類型的一個函數

C++實現:

 1 #include <iostream>
 2 using namespace std;
 3 void int2str(int n, char *str){
 4     char buf[10] = "";
 5     int i = 0;
 6     int len = 0;
 7     int temp = n < 0 ? -n: n;  // temp為n的絕對值
 8     if(str == NULL){
 9         return;
10     }
11 
12     while(temp){
13     buf[i++] = (temp % 10) + 0;  //把temp的每一位上的數存入buf
14     temp = temp / 10;
15     }
16     len = n < 0 ? ++i: i;  //如果n是負數,則多需要一位來存儲負號
17     str[i] = 0;            //末尾是結束符0
18     while(1){
19         i--;
20         if(buf[len-i-1] ==0){
21             break;
22         }
23     str[i] = buf[len-i-1];  //把buf數組裏的字符拷到字符串
24     }
25     if (i == 0){
26         str[i] = -;          //如果是負數,添加一個負號
27     }
28 }
29 
30 int main(){
31     int nNum;
32     char p[10];
33     cout << "Please input an integer:";
34     cin >> nNum;
35     cout << "output: " ;
36     int2str(nNum, p);        //整型轉換成字符串
37     cout<< p << endl;
38     return 0;
39 }

C語言itoa()函數和atoi()函數詳解(整數轉字符C實現)