zoukankan      html  css  js  c++  java
  • 总结:string,char*,CString,int,WCHAR*之间的相互转换:

    --------------------------按我的条理整理的,直接可以赋值的写在最后,其他的方法都是在此基础上得来的----------------------------

    以下用pcstr表示const char*(LPCTSTR),pstr表示char*(LPSTR),用cstr表示Cstring,用str表示string,i表示int

    string转char*
    1.pcstr=str.c_str();//有"\0"
    2.pcstr=str.data();//没有"\0"
    3.str.copy(pstr,str.length(),0);//没有"\0",要先memset

    CString转char*
    1.pstr=cstr.GetBuffer(0);
    2._tscpy(pstr,cstr);
    3.pstr=(LPTSTR)(LPCTSTR)cstr;

    CString转TCHAR*

    char* CString2Str(CString& str)
    {
          char* ret;
          TCHAR* tmp;
          int len = str.GetLength();
          tmp = new TCHAR[len+1];
          ret = new char[len+1];
          memset(ret, 0, len+1);
          memcpy(tmp, str.GetBuffer(), len*sizeof(TCHAR));
          str.ReleaseBuffer();
          tmp[len] = 0;
    #ifdef UNICODE
        WideCharToMultiByte(CP_ACP, NULL, tmp, len, ret, len+1, NULL, NULL);
    #else
        strcpy(ret, (char*)tmp);
    #endif
        delete tmp;
        return ret;
    } 

    CString转WCHAR*
    AllocSysString

    char*转WCHAR*
    MultiByteToWideChar()

    WCHAR *转char*
    WideCharToMultiByte()
    wcstombs()

    各种转int
    1.i=atoi(_in const char* src);

    int转各种
    1.cstr.format();
    2.itoa(_in int val,_out char* dst,_in int radix)

    输出:
    都可以cout直接输出

    赋值:
    string可以接受CString,char*
    CString可以接受char*

    加法:
    string可以加CString,char*
    CString可以加char*

    越界:
    CString抛出异常
    string没有

    比较:
    string,CString不可以比较,都可以与char*比较

  • 相关阅读:
    RDD的基本命令
    python 数据类型
    RDD基础
    sql优化
    python文件操作
    Python之xlsx文件与csv文件相互转换
    ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling
    python
    python操作dataFrame
    python 列表,元祖,字典
  • 原文地址:https://www.cnblogs.com/sdqxcxh/p/1798960.html
Copyright © 2011-2022 走看看