1.先说一下,cstring转char*
网上找到的方法如下:
CString strFileRecord = "C:\haha.flv";
强调一点,因为我是unicode编码,所以如下:
WCHAR* wpFileName =(WCHAR*)m_strFileRecord.GetBuffer(m_strFileRecord.GetLength());
这样出来的filename是完成的路径。
如果我换成:
char* pFileName =(char*)m_strFileRecord.GetBuffer(m_strFileRecord.GetLength());
那么出来的字符就是:“C”,变成一个字符了。
2.因为出现了如上所示问题,所以我就需要wchar和char的互转方法了,如下所示:
std::wstring CharToWchar(const char* c, size_t m_encode = CP_ACP)
{
std::wstring str;
int len = MultiByteToWideChar(m_encode, 0, c, strlen(c), NULL, 0);
wchar_t* m_wchar = new wchar_t[len + 1];
MultiByteToWideChar(m_encode, 0, c, strlen(c), m_wchar, len);
m_wchar[len] = '\0';
str = m_wchar;
delete m_wchar;
return str;
}
std::string WcharToChar(const wchar_t* wp, size_t m_encode = CP_ACP)
{
std::string str;
int len = WideCharToMultiByte(m_encode, 0, wp, wcslen(wp), NULL, 0, NULL, NULL);
char *m_char = new char[len + 1];
WideCharToMultiByte(m_encode, 0, wp, wcslen(wp), m_char, len, NULL, NULL);
m_char[len] = '\0';
str = m_char;
delete m_char;
return str;
}
在此需要感谢博主帮助:http://blog.csdn.net/lightspear/article/details/54695123
先记录一下,之后再整理。