string && wstring
来源:互联网 发布:淘宝免运费怎么设置 编辑:程序博客网 时间:2024/06/06 10:08
#include <string>#define _AFXDLL#include <afx.h>using namespace std;inline 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;}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;}inline void Ascii2WideString( const std::string& szStr, std::wstring& wszStr ){int nLength = MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, NULL, NULL );wszStr.resize(nLength);LPWSTR lpwszStr = new wchar_t[nLength];MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, lpwszStr, nLength );wszStr = lpwszStr;delete [] lpwszStr;}int _tmain(int argc, _TCHAR* argv[]){char* pChar = "我喜欢char";wchar_t* pWideChar = L"我讨厌wchar_t";wchar_t tagWideCharList[100] ;char ch = 'A';char tagChar[100] = {NULL};CString cStr;std::string str;setlocale(LC_ALL,"chs");pWideChar = AnsiToUnicode( pChar );printf( "%ls/n", pWideChar ); wcscpy ( tagWideCharList, pWideChar );wprintf( L"%s/n", tagWideCharList );pWideChar = tagWideCharList;wprintf( L"%s/n", pWideChar );str.insert( str.begin(), ch );cout << str << endl;pWideChar = new wchar_t[str.length()];swprintf( pWideChar, L"%s", str.c_str());wprintf( L"%s/n", pWideChar );pChar = const_cast<char*>(str.c_str());cout << pChar << endl;str = std::string(pChar);cout << str << endl;str = "无聊啊无聊";strcpy( tagChar, str.c_str() );printf( "%s/n", tagChar );cStr = str.c_str();str = string(cStr.GetBuffer(cStr.GetLength()));cStr = pChar;pChar = cStr.GetBuffer( cStr.GetLength() );strncpy( tagChar, (LPCTSTR)CString, sizeof(tagChar));pWideChar = cStr.AllocSysString();printf( "%ls/n", pWideChar );}