日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...

發布時間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。