(原創) 如何將int轉string? (C/C++) (C)

导读:
  C/C++並沒有提供內建的int轉string函數,這裡提供幾個方式達到這個需求。
  1.若用C語言,且想將int轉char *,可用sprintf(),sprintf()可用類似printf()參數轉型。
  1
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  /**//* 2
   InBlock.gif
  (C) OOMusou 2007 http://oomusou.cnblogs.com
  3
   InBlock.gif
  4
   InBlock.gif
  Filename : int2str_sprintf.cpp 5
   InBlock.gif
  Compiler : Visual C++ 8.0 / ANSI C 6
   InBlock.gif
  Description : Demo the how to convert int to const char * 7
   InBlock.gif
  Release : 01/06/2007 1.0 8
   ExpandedBlockEnd.gif
  */
  9
   None.gif
  #include "stdio.h"
  10
   None.gif
  11
   None.gif
  void int2str(int , char *);12
   None.gif
  13
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  int main()
   dot.gif
  {14
   InBlock.gif
  int i = 123
  15
   InBlock.gif
  char s[64];16
   InBlock.gif
  int2str(i, s);17
   InBlock.gif
  puts(s);18
   ExpandedBlockEnd.gif
  }
  19
   None.gif
  20
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  void int2str(int i, char *s)
   dot.gif
  {21
   InBlock.gif
  sprintf(s,"%d",i);22
   ExpandedBlockEnd.gif
  }
  2.若用C語言,還有另外一個寫法,使用_itoa(),Microsoft將這個function擴充成好幾個版本,可參考MSDN Library。
  1
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  /**//* 2
   InBlock.gif
  (C) OOMusou 2007 http://oomusou.cnblogs.com
  3
   InBlock.gif
  4
   InBlock.gif
  Filename : int2str_itoa.cpp 5
   InBlock.gif
  Compiler : Visual C++ 8.0 / ANSI C 6
   InBlock.gif
  Description : Demo the how to convert int to const char * 7
   InBlock.gif
  Release : 01/06/2007 1.0 8
   ExpandedBlockEnd.gif
  */
  9
   None.gif
  #include "stdio.h" // puts()
  10
   None.gif
  #include "stdlib.h" // _itoa()
  11
   None.gif
  
  12
   None.gif
  void int2str(int , char *);13
   None.gif
  14
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  int main()
   dot.gif
  {15
   InBlock.gif
  int i = 123
  16
   InBlock.gif
  char s[64];17
   InBlock.gif
  int2str(i, s);18
   InBlock.gif
  puts(s);19
   ExpandedBlockEnd.gif
  }
  20
   None.gif
  21
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  void int2str(int i, char *s)
   dot.gif
  {22
   InBlock.gif
  _itoa(i, s, 10);23
   ExpandedBlockEnd.gif
  }
  3.若用C++,stringstream是個很好用的東西,stringstream無論是<<或>>,都會自動轉型,要做各型別間的轉換,stringstream是個很好的媒介。
  1
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  /**//* 2
   InBlock.gif
  (C) OOMusou 2007 http://oomusou.cnblogs.com
  3
   InBlock.gif
  4
   InBlock.gif
  Filename : int2str_sstream.cpp 5
   InBlock.gif
  Compiler : Visual C++ 8.0 / ISO C++ 6
   InBlock.gif
  Description : Demo the how to convert int to string 7
   InBlock.gif
  Release : 01/06/2007 1.0 8
   ExpandedBlockEnd.gif
  */
  9
   None.gif
  10
   None.gif
  #include
  11
   None.gif
  #include
  12
   None.gif
  #include
  13
   None.gif
  14
   None.gif
  using namespace std;15
   None.gif
  16
   None.gif
  string int2str(int &);17
   None.gif
  18
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  int main(void)
   dot.gif
  {19
   InBlock.gif
  int i = 123
  20
   InBlock.gif
  string s;21
   InBlock.gif
  s = int2str(i);22
   InBlock.gif
  23
   InBlock.gif
  cout << s << endl;24
   ExpandedBlockEnd.gif
  }
  25
   None.gif
  26
   ExpandedBlockStart.gif
  
   ContractedBlock.gif
  string int2str(int &i)
   dot.gif
  {27
   InBlock.gif
  string s;28
   InBlock.gif
  stringstream ss(s);29
   InBlock.gif
  ss << i;30
   InBlock.gif
  31
   InBlock.gif
  return ss.str();32
   ExpandedBlockEnd.gif
  }
  4.若用C++,據稱boost有更好的方法,不過我還沒有裝boost,所以無從測試
   See Also
  (原創) 如何将int,double转std::string? (C/C++) (template)
  (原創) 如何将std::string转int,double? (C/C++) (template)
   Reference
  http://www.cppblog.com/forLinda/archive/2006/03/17/4298.html

本文转自
http://www.cnblogs.com/oomusou/archive/2007/01/06/613413.html

你可能感兴趣的:(c,compiler,string,microsoft,reference,library)