1. 程式人生 > >整型轉換成字串

整型轉換成字串

本文轉載自:http://blog.csdn.net/mnorst/article/details/6622293

1,使用c語言函式

  將整形數value轉換為其等價的字串 
  char *itoa(int value, char *string, int radix) 


  Parameters(引數說明) 
  第一個引數:value        Number to be converted(將要被轉換的值) 
  第二個引數:string       String result(轉換的結果) 
  第三個引數:radix        Base of value; must be in the range 2 – 36(轉換的基數,取值範圍2-36。例如  radix=10表示10進位制,radix=8表示8進位制。) 
  返回值:與string引數相同,便於函式的巢狀呼叫

  標頭檔案 stdlib.h 

  舉例說明:

  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3. int main()     
  4. {    
  5.     int number = 123456;     
  6.     char string[25];     
  7.     itoa(number, string, 10);     
  8.     printf("integer = %d string = %s\n", number, string);     
  9.     return 0;     
  10. }   

2,  使用CString格式化字串

        函式原型:void  CString::Format( UINT nFormatID, [, argument]...);

        使用方法同printf

舉例說明:

  1. int num = 2;   
  2. CString str;  
  3. str.Format("a=%d",a);  

3,使用sprintf格式化字元


    函式原型: int sprintf( char *buffer, const char *format [, argument] ... ); 

    舉例說明: 

  1. char buffer[10];     
  2. int  num = 3;      
  3. sprintf(buffer,"size=%d",num);     
4,不適用庫函式
  1. /*整數轉化成字串*/
  2. char *IntToStr(int num, char str[])  
  3. {  
  4.     int i = 0, j = 0;  
  5.     char temp[100];  
  6.     while(num)  
  7.     {  
  8.         temp[i] = num % 10 + '0';   //取模運算得到從後往前的每一個數字變成字元
  9.         num = num / 10;  
  10.         i++;  
  11.     }  
  12.     temp[i] = 0;    //字串結束標誌
  13.     i = i - 1;     //回到temp最後一個有意義的數字
  14.     while(i >= 0)  
  15.     {  
  16.         str[j] = temp[i];  
  17.         i--;  
  18.         j++;  
  19.     }  
  20.     str[j] = 0;   //字串結束標誌
  21.     return str;  
  22. }  
  23. /*字串轉換為整數,僅考慮十進位制,不考慮非法字元*/
  24. int StrToInt(char *str)  
  25. {  
  26.     int value = 0;  
  27.     int sign = 1;  
  28.     assert(str != NULL);  
  29.     if(*str == '-')  
  30.     {  
  31.         sign = -1;  
  32.         str++;  
  33.     }elseif(*str == '+')  
  34.     {  
  35.         str++;  
  36.     }  
  37.     while(*str)  
  38.     {  
  39.         value = value * 10 +(*str - '0');  
  40.         str++;  
  41.     }  
  42.     return sign * value;  
  43. }  
  44. /*字串轉換整數,考慮16進位制,10進位制,8進位制,不考慮其他非法字元*/
  45. int StrToIntAll(char *str)  
  46. {  
  47.     int value = 0;  
  48.     int sign = 1;  
  49.     int radix;  
  50.     assert(str != NULL);  
  51.     if(*str == '-')  
  52.     {  
  53.         sign = -1;  
  54.         str++;  
  55.     }  
  56.     elseif(*str == '+')  
  57.     {  
  58.         str++;  
  59.     }  
  60.     //考慮不同的進位制
  61.     if(*str == '0' && (*(str+1) == 'X' || *(str+1) == 'x'))  
  62.     {  
  63.         radix = 16;  
  64.         str += 2;  
  65.     }elseif(*str == '0')  
  66.     {  
  67.         radix = 8;  
  68.         str++;  
  69.     }else
  70.     {  
  71.         radix = 10;  
  72.     }  
  73.     while(*str)  
  74.     {  
  75.         if(radix == 16)  
  76.         {  
  77.             if(*str >= '0' && *str <= '9')  
  78.             {  
  79.                 value = value * radix + (*str - '0');  
  80.             }else
  81.             {  
  82.                 value = value * radix +(*str - 'a' + 10);  
  83.             }  
  84.         }else
  85.         {  
  86.             value = value * radix + (*str - '0');  
  87.         }  
  88.         str++;  
  89.     }  
  90.     return sign * value;  
  91. }