wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...
轉載:http://blog.csdn.net/chocolateconanlan/article/details/4058868
?
wchar_t*,wchar_t,wchat_t數組,char,char*,char數組,std::string,std::wstring,CString….
一些轉換函數,主要針對寬字符。字符串是根本啊,要好好掌握了
#include <string>
// 使用CString必須使用MFC,并且不可包含<windows.h>
#define _AFXDLL
#include <afx.h>
using namespace std;
//———————————————————————————-
//將 單字節char* 轉換為 寬字節 wchar*
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;
}
//———————————————————————————-
// 將 寬字節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;
}
//———————————————————————————-
// 將單字符 string 轉換為寬字符 wstring
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;
// 注:設置語言環境以便輸出WideChar
setlocale(LC_ALL,”chs”);
// 注: char* 轉換 wchar_t*
// 注: wchar_t 未重載 << ,所以不可使用 cout << 輸出
pWideChar = AnsiToUnicode( pChar );
// 注:printf(”%ls”) 和 wprintf(L”%s”) 一致
printf( “%ls/n”, pWideChar );
// 注:wchar_t* 轉換 wchar_t[]
wcscpy ( tagWideCharList, pWideChar );
wprintf( L”%s/n”, tagWideCharList );
// 注:wchar_t[] 轉換 wchar_t*
pWideChar = tagWideCharList;
wprintf( L”%s/n”, pWideChar );
// 注:char 轉換 string
str.insert( str.begin(), ch );
cout << str << endl;
// 注:wchar_t* 轉換 string
pWideChar = new wchar_t[str.length()];
swprintf( pWideChar, L”%s”, str.c_str());
wprintf( L”%s/n”, pWideChar );
// 注:string 轉換 char*
pChar = const_cast<char*>(str.c_str());
cout << pChar << endl;
// 注:char* 轉換 string
str = std::string(pChar);
// 注: cout 的 << 重載了string, 若printf 的話必須 printf(”%s”, str.c_str());
//?? 而不可 print( “%s”, str ); 因為 str 是個 string 類
cout << str << endl;
// 注:string 轉換 char[]
str = “無聊啊無聊”;
strcpy( tagChar, str.c_str() );
printf( “%s/n”, tagChar );
// 注:string 轉換 CString;
cStr = str.c_str();
// 注:CString 轉換 string
str = string(cStr.GetBuffer(cStr.GetLength()));
// 注:char* 轉換 CString
cStr = pChar;
// 注:CString 轉換 char*
pChar = cStr.GetBuffer( cStr.GetLength() );
// 注:CString 轉換 char[]
strncpy( tagChar, (LPCTSTR)CString, sizeof(tagChar));
// 注:CString 轉換 wchar_t*
pWideChar = cStr.AllocSysString();
printf( “%ls/n”, pWideChar );
}
轉載于:https://www.cnblogs.com/fuyanwen/p/3200886.html
總結
以上是生活随笔為你收集整理的wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用TortoiseSVN获取最新版本的
- 下一篇: QTime使用中遇到的”not enou