[转] 各种数据类型转换

来源:互联网 发布:怎么找到我的淘宝店铺 编辑:程序博客网 时间:2024/04/30 10:56

1、CString转char*

    (LPTSTR)(LPCTSTR)str,即为char*类型”

    char*   sz   =   str.GetBuffer(str.GetLength());

    char*   pBuf   =   str.GetBuffer(0);

2、CString 转int

     ansi环境,使用atoi
     unicode环境,使用_ttoi

    CString str = "";
   long m_count = atoi(str.GetBuffer(str.GetLength());

  

   int i;
   i=atoi(str);  //如果str="123",那么现在i的值就是 123;

3、float 整数,小数部分分开

    只计算3位小数的话,加上0.0005即可。令建议你使用函数modf。例:  
   include   <math.h>  
   void   main()  
   {  
      float   a,a_f,a_i;  
      int   m,n;  
      a=1.234;  
      a_f   =   modf(a,   &a_i);  
      m=a_i;  
      n=(a_f+0.0005)*1000;  
     printf("m=%d/nn=%d/n",m,n);  
  } 

4

CString转float的方法:  
  float   i   =   (float)atof(str.GetBuffer(str.GetLength()));  
  如果转换成double,就不用float强制转换了。

float转成   CString   是不是   str.format("%.1f",i) //保留1位小数

5、double 转float

double   a=2.22;  
  CString   str;  
  str.Format("%.2lf",a);