C++ 中 int、 char、 char*、 const char*、 string之间的转换

雨点打透心脏的1/2处 2022-08-21 05:37 319阅读 0赞
  1. char* to string

string s(char *); 注:在不是初始化的地方最好用assign(). !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2. string to const char* string a=”strte”; const char* r=a.c_str(); 注意是const的。还要转到char*: ~~~~~~~~~~ 2.2. const char* to char* const char* r=”123”; char *p1 = new char[strlen(r)+1]; strcpy(p1,r); 附:http://hi.baidu.com/cfans/blog/item/06970ef4b671f366dcc4745d.html 这个页面是具体讲述区别的。 ············································································································· 3. cstring to string vs2005 Unicode下: CStringW str(L”test”); CStringA stra(str.GetBuffer(0)); str.ReleaseBuffer(); std::string strs (stra.GetBuffer(0)); stra.ReleaseBuffer();

非Unicode下:
CString cs(“test”);
std::string str=cs.getBuffer(0);
cs.ReleaseBuffer();

注:GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
++++++++++++++++++++++++++++++++++++++++++++++++++++

  1. double ,int to string
    #include
    using namespace std;

stringstream ss;
string result;
long n=11111;
stream << n; //从long型数据输入
stream >>result; //转换为 string

===================================================

5.char* to int, double ,long

char *s; double x; int i; long l;

s = “ -2309.12E-15”;
x = atof( s );
printf( “atof test: ASCII string: %s\tfloat: %e\n”, s, x );

s = “7.8912654773d210”;
x = atof( s );
printf( “atof test: ASCII string: %s\tfloat: %e\n”, s, x );

s = “ -9885 pigs”;
i = atoi( s );
printf( “atoi test: ASCII string: %s\t\tinteger: %d\n”, s, i );

s = “98854 dollars”;
l = atol( s );
printf( “atol test: ASCII string: %s\t\tlong: %ld\n”, s, l );
-———————————————————————————————————————————————-

  1. string to int ,long ,double
    int s;
    string str=”123”;
    stringstream ss;
    ss<>s;//输出到int
    ss.clear();

——————————————————————————————————————————

  1. date to string
    #include

char dateStr [9];
char timeStr [9];
_strdate( dateStr);
printf( “The current date is %s \n”, dateStr);
_strtime( timeStr );
printf( “The current time is %s \n”, timeStr);

-———-实践证明是正确的版本———————————————————————————————

  1. #include <iostream>
  2. #include <ctime>
  3. #include <cerrno>
  4. int main()
  5. {
  6. //Find the current time
  7. time_t curtime = time(0);
  8. //convert it to tm
  9. tm now=*localtime(&curtime);
  10. //BUFSIZ is standard macro that expands to a integer constant expression
  11. //that is greater then or equal to 256. It is the size of the stream buffer
  12. //used by setbuf()
  13. char dest[BUFSIZ]={
  14. 0};
  15. //Format string determines the conversion specification's behaviour
  16. const char format[]="%A, %B %d %Y. The time is %X";
  17. //strftime - converts date and time to a string
  18. if (strftime(dest, sizeof(dest)-1, format, &now)>0)
  19. std::cout<<dest<<std::endl;
  20. else
  21. std::cerr<<"strftime failed. Errno code: "<<errno<<std::endl;
  22. }

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
8.string to cstring

+++++++++++++++++++++++++++++++++++++++++++++++++++++
非Unicode下:
int 转 CString:
CString.Format(“%d”,int);
………………………….
string 转 CString
CString.format(“%s”, string.c_str());
用c_str()确实比data()要好.
…………………………………
char* 转 CString
CString.format(“%s”, char*);
CString strtest;
char * charpoint;
charpoint=”give string a value”;
strtest=charpoint; //直接付值
……………………………………………..
CString 转 int
CString ss=”1212.12”;
int temp=atoi(ss); //atoi _atoi64或atol
…………………………………………………………………………………………………………………..
9.在Unicode下的CString to double
CSting sTemp(“123.567”);
double dTemp = _wtof(

发表评论

表情:
评论列表 (有 0 条评论,319人围观)

还没有评论,来说两句吧...

相关阅读