码迷,mamicode.com
首页 > 其他好文 > 详细

基本字符类型的转换

时间:2017-05-30 19:28:20      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:char   []   box   转换   atoi   c_str   小数   基本   十进制   

一、int <---> CString

  int转CString:

    int i=100;

    CString str="";

    str.Format("%d",i);

  CString转int:

    CString str="123";

    int i;

    i=atoi(str);

    //i=_ttoi(str);

二、float <---> CString

  float转CString:

    float a=3.1415;

    CString str="";

    str.Format("%.2f",a); //保留小数点后两位

  CString转float:

    先将CString转char*,因为atof()的参数为char*型,atof()的返回值为double型,可直接强转为float型;

    CString str="3.1415";

    char* s;

    s=str.GetBuffer(str.GetLength());

    str.ReleaseBuffer();

    float f;

    f=atof(s);

三、double <---> CString

  double转CString:

    double d=3.1415;

    CString str="";

    str.Format("%.2lf",d);

  CString转double:

    CString str="3.1415";

    char* s;

    s=str.GetBuffer(str.GetLength());

    str.ReleaseBuffer();

    double d;

    d=atof(s);

四、string <---> CString

  string转CString:

    CString str;

    string s="hello";

    str.Format("%s",s.c_str());

  CString转string:

    CString str="hello";

    string s(str.GetBuffer());

五、char* <---> CString

  char*转CString:

    char sChar[]="hello";

    CString str;

    str.Format("%s",sChar);

    AfxMessageBox(str);

  CString转char*:

    方法一:

    CString str="hello";

    char* s;

    s=new char[str.GetLength()+1];

    strcpy(s,str);

    方法二:

    CString str="hello";

    char* s;

    s=str.GetBuffer(str.GetLength());

    str.ReleaseBuffer();

六、TCHAR* <---> CString

  TCHAR*转CString:

    TCHAR sChar[]="hello";

    CString str;

    str.Format("%s",sChar);

    AfxMessageBox(str);

  CString转TCHAR*:

    方法一:

    CString str="hello";

    TCHAR* s=(LPTSTR)(LPCTSTR)str;

    方法二:

    CString str="hello";

    TCHAR* s;

    s=new TCHAR[str.GetLength()+1];

    _tcscpy(s,str);

    方法三:

    CString str="hello";

    TCHAR* s;

    s=str.GetBuffer(str.GetLength());

    str.ReleaseBuffer();

七、int <---> char*

  int转char*:

    int i=54321;

    char ch[5];

    itoa(i,ch,10);  //10-十进制,2-二进制,8-八进制,16-十六进制

 

基本字符类型的转换

标签:char   []   box   转换   atoi   c_str   小数   基本   十进制   

原文地址:http://www.cnblogs.com/zhouwanqiu/p/6920918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!