C++ 常见的类型转换

1、CString——>string

/*注意,一定要加USES_CONVERSION   包含头文件#include "afxpriv.h" */
1、USES_CONVERSION; 
    CString a = FilePathName; 
    string  b; 
    char* p = T2A(a); 
    b = p;
2、CString CstrInputFile = FilePathName;
    std::string StrIputFile( CW2A( CstrInputFile.GetString()));

2、CString——>const char*

   CString a = FilePathName; 
    const char* p = T2A(a); 

3、输出int、float类型

   CString str;
   Str.Format(_T(“%d”),width);  
   Str.Format(_T(“%f”),width);

4、CString->float

   CString b;
   float a=(float)wcstod(b,NULL);
   CString->int
   CString b;
   int a=_wtoi(b);
   const char*->int
   const char* b;
   int a=_atoi(b);

5、 TCHAR->float

TCHAR scale[10]={0};
float n = _ttof(scale);

6、TCHAR->string

std::string TCHAR2STRING(TCHAR *tstr)
{
      int iLen = WideCharToMultiByte(CP_ACP, 0,tstr, -1, NULL, 0, NULL, NULL);
      char* chRtn =new char[iLen*sizeof(char)];
      WideCharToMultiByte(CP_ACP, 0, tstr, -1, chRtn, iLen, NULL, NULL);
      std::string str(chRtn);
      return str;
}

7、TCHAR->char

char* tchartochar(TCHAR *tchar)
{
      LPSTR pszOut = NULL;

      if(tchar != NULL){
            int nInputStrLen = wcslen(tchar);

            int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, tchar, nInputStrLen, NULL, 0, 0, 0) + 2;

            pszOut = new char[nOutputStrLen];

            if(pszOut){
                  memset(pszOut, 0x00, nOutputStrLen);
                 WideCharToMultiByte(CP_ACP, 0, tchar, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
            }
      }
      return pszOut;
}

8.stringtotchar

TCHAR* stringtotchar(string s)   //Change string to TCHAR
{
    //TCHAR *tchar=_T("");
    static TCHAR tchar[256]=_T("");
    _tcscpy_s(tchar, 256 , CA2T(s.c_str()));        // s.length()
    return tchar;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 中的隐式类型转换是指在某些情况下,编译器会自动将一种数据类型转换为另一种数据类型。隐式类型转换可以让代码更简洁,但也容易引起一些问题,例如精度丢失和类型不匹配等问题。以下是一些常见的隐式类型转换: 1. 整数类型的隐式C++ 中的整数类型有很多种,例如 int、long、short、char 等。在进行赋值或运算时,如果两个整数类型不同,编译器会自动将其中一个整数类型转换为另一个整数类型。例如: ```cpp int a = 10; long b = a; // int 换为 long ``` 2. 浮点数类型的隐式C++ 中的浮点数类型有 float、double 等。在进行赋值或运算时,如果两个浮点数类型不同,编译器会自动将其中一个浮点数类型转换为另一个浮点数类型。例如: ```cpp float a = 1.23; double b = a; // float 换为 double ``` 3. 数组类型的隐式C++ 中的数组类型可以通过指针隐式换为另一种数组类型。例如: ```cpp int a[10]; double* b = a; // int* 换为 double* ``` 4. 类类型的隐式C++ 中的类类型可以定义自己的换函数和换构造函数,在一些情况下可以进行隐式类型转换。例如: ```cpp class MyInt { public: MyInt(int n) : m_n(n) {} operator int() { return m_n; } // 换函数 private: int m_n; }; void foo(int n) { std::cout << n << std::endl; } int main() { MyInt a(10); foo(a); // MyInt 换为 int return 0; } ``` 在这个示例中,我们定义了一个 MyInt 类,它可以通过换函数 operator int() 将自己换为 int 类型。在 main() 函数中,我们将一个 MyInt 对象传给了一个函数,编译器会自动调用 operator int() 进行隐式类型转换。 需要注意的是,隐式类型转换虽然方便,但也容易引起一些问题,因此在实际编程中需要谨慎使用。为了避免出现类型不匹配的问题,可以使用显式类型转换来明确地指定需要换的类型。例如: ```cpp double a = 1.23; int b = static_cast<int>(a); // 显式将 double 换为 int ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值