MFC 類型轉(zhuǎn)換
一、CString與LPSTR、LPCSTR、string、 char*相互轉(zhuǎn)換
1. CString轉(zhuǎn)換成LPSTR:方法一:
1 CString strFileName;
2 LPSTR lpStr = strFileName.GetBuffer();
3 strFileName.ReleaseBuffer();
方法二:
1 CString strFileName;
2 LPSTR lpStr = (LPSTR)(LPCSTR)strFimeName;
3
2. LPSTR轉(zhuǎn)換成CString:
1 LPSTR lpStr = _T("TestStr");
2 CString str(lpStr);
3 //注意:CString和LPCSTR可直接轉(zhuǎn)換,如下:
4 CString str;
5 LPCSTR lpcStr = (LPCSTR)str;
CString轉(zhuǎn)換成LPSTR:
CString strTemp;
LPTSTR pSterTemp = strTemp.GetBuffer(); //這會為pSetTemp指向的字符串分配內(nèi)存,同時(shí)會把strTemp的內(nèi)容拷貝下來。。
strTemp.ReleaseBuffer(); //must call this funtion. 歸還內(nèi)存給操作系統(tǒng)。
或:
LPTSTR pSterTemp = (LPTSTR)(LPCSTR)strTemp;
3.string 轉(zhuǎn) CString
1 CString.format("%s", string.c_str());
4.CString 轉(zhuǎn) string
1 string s(CString.GetBuffer(CString.GetLength()));
5. CString轉(zhuǎn)char*
(1)傳給未分配內(nèi)存的const char* (LPCTSTR)指針.
1 CString cstr(asdd);
2 const char* ch = (LPCTSTR)cstr;
3 //ch指向的地址和cstr相同。但由于使用const保證ch不會修改,所以安全.
(2)傳給未分配內(nèi)存的指針.
CString cstr = "ASDDSD";
char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
cstr.ReleaseBuffer();
//修改ch指向的值等于修改cstr里面的值.
//PS:用完ch后,不用delete ch,因?yàn)檫@樣會破壞cstr內(nèi)部空間,容易造成程序崩潰.
(3)第二種用法。把CString 值賦給已分配內(nèi)存的char *.
1 CString cstr1 = "ASDDSD";
2 int strLength = cstr1.GetLength() + 1;
3 char *pValue = new char[strLength];
4 strncpy(pValue, cstr1, strLength);
(4)第三種用法.把CString 值賦給已分配內(nèi)存char[]數(shù)組.
1 CString cstr2 = "ASDDSD";
2 int strLength1 = cstr1.GetLength() + 1;
3 char chArray[100];
4 memset(chArray,0, sizeof(bool) * 100); //將數(shù)組的垃圾內(nèi)容清空.
6、char * 轉(zhuǎn) CString
1 CString.format("%s", char*);
二、string與char*轉(zhuǎn)換
1、char * 轉(zhuǎn) string
1 string s(char *);
2、string 轉(zhuǎn) char *
1 char *p = string.c_str();
三、CString轉(zhuǎn)換到TCHAR *
1、使用強(qiáng)制轉(zhuǎn)換。例如:
1 CString theString( "This is a test" );
2 LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;
2、使用strcpy。例如:
1 CString theString( "This is a test" );
2 LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
3 _tcscpy(lpsz, theString);
4
5 //需要說明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二個(gè)參數(shù)是 const wchar_t* (Unicode)或const char* (ANSI),系統(tǒng)編譯器將會自動對其進(jìn)行轉(zhuǎn)換。
3、使用CString::GetBuffer。例如:
1 CString s(_T("This is a test "));
2 LPTSTR p = s.GetBuffer();
3 // 在這里添加使用p的代碼
4 if(p != NULL) *p = _T('\0');
5 s.ReleaseBuffer();
6 // 使用完后及時(shí)釋放,以便能使用其它的CString成員函數(shù)
四、string轉(zhuǎn)wstring
1 wchar_t wcs[100], wc;
2
3 string testStr1="1234567890";
4
5 setlocale(LC_CTYPE, ""); //很重要,沒有這一句,轉(zhuǎn)換會失敗
6
7 mbstowcs(wcs, testStr1.c_str(), 99);
8
9 wstring testStr(wcs);
#include <string>
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
std::string result = _Dest;
delete []_Dest;
_Dest = NULL;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
std::wstring s2ws(const std::string& s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
_Dest = NULL;
setlocale(LC_ALL, "C");
return result;
}
C語言庫函數(shù)名: atoi 功 能: 把字符串轉(zhuǎn)換成整型數(shù)
函數(shù)名: atof 功 能: 把字符串轉(zhuǎn)換成浮點(diǎn)數(shù)
CString與Byte數(shù)組相互轉(zhuǎn)換
CString cs1 = "gettruckpos";
byte buf[200];
memcpy(buf,cs1.GetBuffer(cs1.GetLength()),cs1.GetLength()); //將cstring放入byte數(shù)組
CString *pPhoneNum =new CString((char*)buf, cs1.GetLength()); //將byte數(shù)組轉(zhuǎn)換成cstring
CString cs2 = *pPhoneNum;
int轉(zhuǎn)byte
//int轉(zhuǎn)byte
void intToByte(int i,byte *bytes,int size = 4)
{
byte[] bytes = new byte[4];
memset(bytes,0,sizeof(byte) * size);
bytes[0] = (byte) (0xff & i);
bytes[1] = (byte) ((0xff00 & i) >> 8);
bytes[2] = (byte) ((0xff0000 & i) >> 16);
bytes[3] = (byte) ((0xff000000 & i) >> 24);
return ;
}
BYTE* IntToBytes(int nNum, BOOL isHighFirst, BYTE *pVal)
{
BYTE result[4] = {0};
if (isHighFirst)
{
result[0] = (BYTE)(nNum >> 24 & 0xff);
result[1] = (BYTE)(nNum >> 16 & 0xff);
result[2] = (BYTE)(nNum >> 8 & 0xff);
result[3] = (BYTE)(nNum & 0xff);
}
else
{
result[3] = (BYTE)(nNum >> 24 & 0xff);
result[2] = (BYTE)(nNum >> 16 & 0xff);
result[1] = (BYTE)(nNum >> 8 & 0xff);
result[0] = (BYTE)(nNum & 0xff);
}
memcpy(pVal, result, 4);
return result;
}
byte轉(zhuǎn)int
//byte轉(zhuǎn)int
int bytesToInt(byte* bytes,int size = 4)
{
int addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}
CString轉(zhuǎn) wchar_t*wchar_t * CCommonFun::ConvertCStringToWchar_t(CString &str)
{
CString sTemp = str;
const char* CStr = (LPCTSTR)sTemp;
size_t len = strlen(CStr) + 1;
size_t converted = 0;
wchar_t *WStr;
WStr = (wchar_t*)malloc(len*sizeof(wchar_t));
mbstowcs_s(&converted, WStr, len, CStr, _TRUNCATE);
return WStr;
}
注意:可以在調(diào)用的地方釋放分配的內(nèi)存,如果遇到漢子轉(zhuǎn)換亂碼問題可以在轉(zhuǎn)換前使用setlocale(LC_ALL, "chs")或者setlocale(LC_ALL,"zh_CN.UTF-8")進(jìn)行設(shè)置wchar_t*轉(zhuǎn)CStringCString CTestTextToPicDlg::ConvertWchar_tToCString(const wchar_t* WStr)
{
size_t len = wcslen(WStr) + 1;
size_t converted = 0;
char *CStr;
CStr=(char*)malloc(len*sizeof(char));
wcstombs_s(&converted, CStr, len, WStr, _TRUNCATE);
CString sTemp;
sTemp.Format("%s", CStr);
free(CStr);
CStr = NULL;
return sTemp;
}
TCHAR和char的區(qū)別:
C++支持兩種字符串,即常規(guī)的ANSI編碼(使用""包裹)和Unicode編碼(使用L""包裹),這樣對應(yīng)的就有了兩套字符串字符串處理函數(shù),比如:strlen和wstrlen,分別用于處理兩種字符串。
由于字符編碼的不同,在C++中有三種對于字符類型:char, wchar_t , TCHAR。其實(shí)TCHAR不能算作一種類型,他緊緊是一個(gè)宏。我們都知道,宏在預(yù)編譯的時(shí)候會被替換成相應(yīng)的內(nèi)容。TCHAR 在使用多字節(jié)編碼時(shí)被定義成char,在Unicode編碼時(shí)定義成wchar_t。
如果你希望同時(shí)為ANSI和Unicode編譯的源代碼,那就要include TChar.h。TCHAR是定義在其中的一個(gè)宏,它視你是否定義了_UNICODE宏而定義成char或者wchar_t。如果你使用了TCHAR,那么就不應(yīng)該使用ANSI的strXXX函數(shù)或者Unicode的wcsXXX函數(shù)了,而必須使用TChar.h中定義的_tcsXXX函數(shù)。另外,為了解決剛才提到帶“L”的問題,TChar.h中定義了一個(gè)宏:“_TEXT”。
以strcpy函數(shù)為例子,總結(jié)一下:
.如果你想使用ANSI字符串,那么請使用這一套寫法:
char szString[100];
strcpy(szString,"test");
.如果你想使用Unicode字符串,那么請使用這一套:
wchar_t szString[100];
wcscpyszString,L"test");
.如果你想通過定義_UNICODE宏,而編譯ANSI或者Unicode字符串代碼:
TCHAR szString[100];
_tcscpy(szString,_TEXT("test"));
使用TCHAR系列方案編寫程序
TCHAR是一種字符串類型,它讓你在以MBCS和UNNICODE來build程序時(shí)可以使用同樣的代碼,不需要使用繁瑣的宏定義來包含你的代碼。
TCHAR的引入,主要是在Tchar.h文件中,該文件包含這方面的重要的定義信息。
對于包含了對str函數(shù)或wcs函數(shù)進(jìn)行顯式調(diào)用的代碼來說,無法非常容易地同時(shí)為ANSI和Unicode對這些代碼進(jìn)行編譯。本章前面說過,可以創(chuàng)建同時(shí)為ANSI和Unicode進(jìn)行編譯的單個(gè)源代碼文件。若要建立雙重功能,必須包含TChar.h文件,而不是包含String.h文件。
TChar.h文件的唯一作用是幫助創(chuàng)建ANSI/Unicode通用源代碼文件。它包含你應(yīng)該用在源代碼中的一組宏,而不應(yīng)該直接調(diào)用str函數(shù)或者 wcs函數(shù)。如果在編譯源代碼文件時(shí)定義了_UNICODE,這些宏就會引用wcs這組函數(shù)。如果沒有定義_UNICODE,那么這些宏將引用str這組宏。
TCHAR的定義如下:
#ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif
所以用MBCS來build時(shí),TCHAR是char,使用UNICODE時(shí),TCHAR是wchar_t。
還有一個(gè)宏來處理定義Unicode字符串常量時(shí)所需的L前綴。
#ifdef UNICODE
#define _T(x) L##x
#define _TEXT(x) L##x
#define __T(x) L##x
#else
#define _T(x) x
#define _TEXT(x) x
#define __T(x) x
#endif
## 是一個(gè)預(yù)處理操作符,它可以把兩個(gè)參數(shù)連在一起。如果你的代碼中需要字符串常量,在它前面加上_T宏。如果你使用Unicode來build,它會在字符串常量前加上L前綴。
TCHAR szNewText[] = _T("we love Bob!");
_UNICODE宏用于C運(yùn)行期頭文件,而UNICODE宏則用于Windows頭文件。當(dāng)編譯源代碼模塊時(shí),通常必須同時(shí)定義這兩個(gè)宏。
像是用宏來隱藏SetWindowTextA/W的細(xì)節(jié)一樣,還有很多可以供你使用的宏來實(shí)現(xiàn)str***()和_mbs***()等字符串函數(shù)。例如,你可以使用_tcsrchr宏來替換strrchr()、_mbsrchr()和wcsrchr()。_tcsrchr根據(jù)你預(yù)定義的宏是_MBCS還是 UNICODE來擴(kuò)展成正確的函數(shù),就象SetWindowText所作的一樣。
不僅str***()函數(shù)有TCHAR宏。其他的函數(shù)如, _stprintf(代替sprinft()和swprintf()),_tfopen(代替fopen()和_wfopen())。 MSDN中"Generic-Text Routine Mappings."標(biāo)題下有完整的宏列表。
QT類型轉(zhuǎn)換
Qstring str;
char* ch;
QByteArray ba = str.toLatin1();
ch=ba.data();
這樣就完成了QString向char*的轉(zhuǎn)化。經(jīng)測試程序運(yùn)行時(shí)不會出現(xiàn)bug
注意第三行,一定要加上,不可以str.toLatin1().data()這樣一部完成,可能會出錯。
補(bǔ)充:以上方法當(dāng)QString里不含中文時(shí),沒有問題,但是QString內(nèi)含有中文時(shí),轉(zhuǎn)換為char*就是亂碼,采用如下方法解決:
方法1:
添加GBK編碼支持:
#include <QTextCodec>
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
然后改變上面的第三行為:QByteArray ba = str.toLoacl8Bit(); toLoacl8Bit支持中文
方法2:
先將QString轉(zhuǎn)為標(biāo)準(zhǔn)庫中的string類型,然后將string轉(zhuǎn)為char*,如下:
std::string str = filename.toStdString();
const char* ch = str.c_str();
posted on 2012-04-06 17:08
王海光 閱讀(1001)
評論(0) 編輯 收藏 引用 所屬分類:
C++