- 最近在看一写PE文件格式的东西,想做一个读取PE文件信息的小工具,中间遇到将LPVOID格式无法转换到LPTSTR格式,强制转换屡试屡败,多显示乱码。我们知道LPVOID格式可以直接转换到char *,最后发现一篇写char*与wchar_t*格式互相转换的文章,引用文中代码转换成功。
- 原帖地址http://www.cnblogs.com/yyxr/archive/2009/10/06/1578458.html
- //将单字节char*转化为宽字节wchar_t*
- wchar_t* AnsiToUnicode( const char* szStr )
- {
- int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
- if (nLen == 0)
- {
- return NULL;
- }
- wchar_t* pResult = new wchar_t[nLen];
- MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
- return pResult;
- }
- //将宽字节wchar_t*转化为单字节char*
- inline char* UnicodeToAnsi( const wchar_t* szStr )
- {
- int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
- if (nLen == 0)
- {
- return NULL;
- }
- char* pResult = new char[nLen];
- WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
- return pResult;
- }