VC++常用数据类型转化

char* 转换成 LPCTSTR

    const char* dibFileName;
    int num = MultiByteToWideChar(0, 0, dibFileName, -1, NULL, 0);
    wchar_t *wide = new wchar_t[num];
    MultiByteToWideChar(0, 0, dibFileName, -1, wide, num);

 

    char m_fileName[256];
    // 这样在多字节或UNICODE模式下都可以。
    _bstr_t bstrTmp(m_fileName);
    LPCTSTR strTmp = (LPTSTR)bstrTmp;

 解析:

num 获得长字节所需的空间
MultiByteToWideChar()表示将s中的字符传递到ps指向的内存中。-1表示传输至s中的'\0'处,num表示传递的字节个数。

char* 转换成 CString

    // char * -->CString
    // outputFilePath = "G:\\testDLL"
    const char* outputFilePath;
    CString str1(outputFilePath);

CString转换成char* 

char * CDib::CStringToCharArray(CString str)
{
    char *ptr;
#ifdef _UNICODE
    LONG len;
    len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
    ptr = new char[len + 1];
    memset(ptr, 0, len + 1);
    WideCharToMultiByte(CP_ACP, 0, str, -1, ptr, len + 1, NULL, NULL);
#else
    ptr = new char[str.GetAllocLength() + 1];
    sprintf(ptr, _T("%s"), str);
#endif
    return ptr;
}

 

posted @ 2016-04-25 13:05  Bobby0322  阅读(257)  评论(0编辑  收藏  举报