• CString、string、char*之间的转化


    CString、string、char*之间的转化

    CString:MFC中定义的字符串类型,有很多很方便的操作函数,如Format函数(可以将各种类型转化为CString类型)
    string:C++中标准字符串类
    char*,char[]:C风格字符串,以''结尾

    1.char*转string

    方法直接转化:

    char* cstr = "Hello!";
    string str;
    str = cstr;
    

    2.string转char*

    利用string类的c_str()或data()函数,这两个函数返回的都是const char*类型,故无法对返回的C风格字符串进行修改。

    string str("Hello!");   //这里其实就包含了小节1,采用的C风格字符串进行string的初始化
    const char* = str.c_str;
    //或者
    const char* cstr= str.data();
    

    注:c_str()与data()区别
      c_str()是标准做法,返回的是C风格字符串(以''结尾),而data()返回的是原始string数据,不一定以''结尾。c_str()中其实调用了data(),不过在调用前进行了判断,如果没有以''结尾则补上'',故在转化C风格字符串时c_str()更保险,不过data()效率更高。
      

    3.CString与char*转化

    这里就要分为两种情况:MBCS编码与Unicode编码

    3.1 MBCS编码:此时CString是CStringA

    (1) CString转char*
    法一:CString直接调用GetBuffer即可将CString转化为char*

    CString cstring= "Hello!";          //MFC中其实等同于 CString cstring = _T("Hello!"); 
    char* cstr = cstring.GetBuffer(0);
    CString。ReleaseBuffer();          //一般使用完GetBuffer要调用ReleaseBuffer
    

    法二:使用LPCTSTR转化,CString中有operator LPCTSTR(){...}转换函数,一般需要const char*时传入CString都会自动调用LPCTSTR()隐式转换

    CString cstring ="Hello";  
    const char *cstr = (LPCSTR)cstring;     //LPCTSTR转换后返回的是const char*类型
    char* *cstr1 = (LPSTR)(LPCSTR)cstring;  //这种方法将 const char* 强制转换成 char* 使用,不安全,不建议使用
    

    (2) char*转CString
    (I)CString的构造函数就可将char*转化为CString;
    (II)直接转换;
    (III)利用Format函数

    char* cstr = "Hello!";
    CString cstring1(cstr);      //(I)
    CString cstring2 = cstr;     //(II)
    CString cstring3;
    cstring.Format(_T("%s"), cstr)  //(III)
    

    3.2 Unicode编码:此时CString是CStringW

    方法:
      (I)使用L转换宏USES_CONVERSION
       头文件MFC下:#include "afxconv.h"
       ATL下:#include "atlconv.h "
      (II)使用windows API: MultiByteToWideChar与WideCharToMultiByte(这里不介绍了)

    (1) CString转char*
    使用T2A或W2A

    CString cstring= L"Hello!";          //MFC中其实等同于 CString cstring = _T("Hello!"); 
    USES_CONVERSION
    char* cstr  = T2A(cstring);
    

    (2) char*转CString
    使用A2T或A2W

    char* cstr = "Hello!";
    USES_CONVERSION
    CString cstring = A2T(cstr);
    

    (3)使用USES_CONVERSION注意事项:它们从堆栈上分配内存,直到调用它的函数返回,该内存不会被释放。故不要在大量调用的地方使用,如循环,否则很容易引起堆栈溢出。

    (4)条件判断转换方法
    经常我们在类型转换时会利用编码方式自动选择转换方法,使程序适应多种编码方式。利用是否定义宏UNICODE和_UNICODE
    UNICODE:用于Windows头文件
    _UNICODE:用于C运行时(CRT)头文件

    CString cstring = _T("Hello!"); 
    #ifndef UNICODE
    char* cstr = cstring.GetBuffer(0);
    #else
    USES_CONVERSION
    CString cstring = T2A(cstr); 
    #endif
    

    当然可以选择利用defined(宏名称)将UNICODE与_UNICODE都加入判断

    CString cstring = _T("Hello!"); 
    #if defined(UNICODE) || defined(_UNICODE)
    char* cstr = cstring.GetBuffer(0);
    #else
    USES_CONVERSION
    CString cstring = T2A(cstr); 
    #endif
    

    补充

      C++中string类的to_string函数可以把多种类型(int、double、float、unsigned int等)转换为string类型。

  • 相关阅读:
    sqlite android
    cocos2dx 2.1.3 使用json
    cocos2dandroid 自动缩放、高清显示
    cocos2dx HttpClient
    coco2dx 2.1.3 之 使用网络请求
    facebook on android
    项目运行出错怎么办?
    Bug应对策略 本文系转
    从网页中导入Excel
    ASP.NET HTTP运行时组成详解[转帖]
  • 原文地址:https://www.cnblogs.com/silentteen/p/7978124.html
Copyright © 2020-2023  润新知