16.1如何將基本數(shù)據(jù)類型轉(zhuǎn)換成CString類型
用CString的Format方法
void CDemoView::OnDraw(CDC* pDC) { int a = 100; double b = 1.23; //將整型轉(zhuǎn)換成CString CString str1 = _T(""); str1.Format(_T("%d"), a); //將實(shí)型轉(zhuǎn)換成CString CString str2 = _T(""); str2.Format(_T("%f"), b); CString strText = _T(""); strText.Format(_T("str1 = %s"), str1); pDC->TextOut(100, 50, strText); strText.Format(_T("str2 = %s"), str2); pDC->TextOut(100, 100, strText); }
16.2如何將CString類型轉(zhuǎn)換成基本數(shù)據(jù)類型
atoi:Convert a string to integer.
參考:
http://baike.baidu.com/view/653935.htmvoid CDemoView::OnDraw(CDC* pDC) { CString str1 = _T("100"); CString str2 = _T("1.23"); //將CString轉(zhuǎn)換成整型 int a = atoi(str1); //將CString轉(zhuǎn)換成實(shí)型 double b = atof(str2); CString strText = _T(""); strText.Format(_T("a = %d"), a); pDC->TextOut(100, 50, strText); strText.Format(_T("b = %f"), b); pDC->TextOut(100, 100, strText); }
16.3如何將TCHAR類型轉(zhuǎn)換成CString類型
void CDemoView::OnDraw(CDC* pDC) { TCHAR sz[] = _T("Hello world!"); //直接賦值 CString str1 = sz; //調(diào)用CString::Format函數(shù) CString str2 = _T(""); str2.Format(_T("%s"), sz); CString strText = _T(""); strText.Format(_T("str1 = %s"), str1); pDC->TextOut(100, 50, strText); strText.Format(_T("str2 = %s"), str2); pDC->TextOut(100, 100, strText); }
16.4如何將CString類型轉(zhuǎn)換成TCHAR類型
void CDemoView::OnDraw(CDC* pDC) { CString str = _T("Hello world!"); //強(qiáng)制轉(zhuǎn)換 LPTSTR psz1 = (LPTSTR)(LPCTSTR)str; //調(diào)用CString::GetBuffer函數(shù) LPTSTR psz2 = str.GetBuffer(str.GetLength()); str.ReleaseBuffer(); CString strText = _T(""); strText.Format(_T("psz1 = %s"), psz1); pDC->TextOut(100, 50, strText); strText.Format(_T("psz2 = %s"), psz2); pDC->TextOut(100, 100, strText); }
16.5如何將TCHAR類型轉(zhuǎn)換成BSTR類型
void CDemoView::OnDraw(CDC* pDC) { TCHAR sz[] = _T("Hello world!"); //調(diào)用ConvertStringToBSTR函數(shù) BSTR bstr1 = _com_util::ConvertStringToBSTR(sz); //使用_bstr_t BSTR bstr2 = _bstr_t(sz); CString strText = _T(""); strText.Format(_T("bstr1 = %s"), (CString)bstr1); pDC->TextOut(100, 50, strText); strText.Format(_T("bstr2 = %s"), (CString)bstr2); pDC->TextOut(100, 100, strText); }
16.6如何將BSTR類型轉(zhuǎn)換成TCHAR類型
void CDemoView::OnDraw(CDC* pDC) { BSTR bstr = L"Hello world!"; //調(diào)用ConvertBSTRToString函數(shù) LPTSTR psz = _com_util::ConvertBSTRToString(bstr); CString strText = _T(""); strText.Format(_T("psz = %s"), psz); pDC->TextOut(100, 50, strText); }
16.7 如何將BSTR類型轉(zhuǎn)換成CString類型
SysAllocString和SysFreeString
void CDemoView::OnDraw(CDC* pDC) { BSTR bstr = ::SysAllocString(L"Hello world!"); //強(qiáng)制轉(zhuǎn)換 CString str = (CString)bstr; CString strText = _T(""); strText.Format(_T("str = %s"), str); pDC->TextOut(100, 50, strText); ::SysFreeString(bstr); }
16.8如何將CString類型轉(zhuǎn)換成BSTR類型
void CDemoView::OnDraw(CDC* pDC) { CString str = _T("Hello world!"); //調(diào)用CString::AllocSysString函數(shù) BSTR bstr = str.AllocSysString(); CString strText = _T(""); strText.Format(_T("bstr = %s"), (CString)bstr); pDC->TextOut(100, 50, strText); ::SysAllocString(bstr); }
16.9 如何將DWORD類型轉(zhuǎn)換成WORD類型
LOWORD和HIWORD
void CDemoView::OnDraw(CDC* pDC) { //將1個(gè)DWORD類型數(shù)據(jù)分解成2個(gè)WORD類型數(shù)據(jù) DWORD dwValue = 0xFFAA5500; WORD wLow = LOWORD(dwValue); WORD wHigh = HIWORD(dwValue); CString strText = _T(""); strText.Format(_T("DWORD:0x%08X"), dwValue); pDC->TextOut(100, 50, strText); strText.Format(_T("low-order word:0x%04X"), wLow); pDC->TextOut(100, 100, strText); strText.Format(_T("high-order word:0x%04X"), wHigh); pDC->TextOut(100, 150, strText); }
16.10 如何將WORD類型轉(zhuǎn)換成BYTE類型
LOBYTE和HIBYTE
void CDemoView::OnDraw(CDC* pDC) { //將1個(gè)WORD類型數(shù)據(jù)分解成2個(gè)BYTE類型數(shù)據(jù) WORD wValue = 0xFF00; BYTE bLow = LOBYTE(wValue); BYTE bHigh = HIBYTE(wValue); CString strText = _T(""); strText.Format(_T("WORD:0x%04X"), wValue); pDC->TextOut(100, 50, strText); strText.Format(_T("low-order byte:0x%02X"), bLow); pDC->TextOut(100, 100, strText); strText.Format(_T("high-order byte:0x%02X"), bHigh); pDC->TextOut(100, 150, strText); }
16.11如何將WORD類型組合成DWORD類型
void CDemoView::OnDraw(CDC* pDC) { //將2個(gè)WORD類型數(shù)據(jù)組合成1個(gè)DWORD類型數(shù)據(jù) WORD wLow = 0x5500; WORD wHigh = 0xFFAA; DWORD dwValue = MAKELONG(wLow, wHigh); CString strText = _T(""); strText.Format(_T("low-order word:0x%04X"), wLow); pDC->TextOut(100, 50, strText); strText.Format(_T("high-order word:0x%04X"), wHigh); pDC->TextOut(100, 100, strText); strText.Format(_T("DWORD:0x%08X"), dwValue); pDC->TextOut(100, 150, strText); }
16.12 如何將BYTE類型轉(zhuǎn)換成WORD類型
void CDemoView::OnDraw(CDC* pDC) { //將2個(gè)BYTE類型數(shù)據(jù)組合成1個(gè)WORD類型數(shù)據(jù) BYTE bLow = 0x00; BYTE bHigh = 0xFF; WORD wValue = MAKEWORD(bLow, bHigh); CString strText = _T(""); strText.Format(_T("low-order byte:0x%02X"), bLow); pDC->TextOut(100, 50, strText); strText.Format(_T("high-order byte:0x%02X"), bHigh); pDC->TextOut(100, 100, strText); strText.Format(_T("WORD:0x%04X"), wValue); pDC->TextOut(100, 150, strText); }
16.13 如何將COLORREF類型轉(zhuǎn)換成RGB分量
void CDemoView::OnDraw(CDC* pDC) { COLORREF cr = RGB(255, 128, 0); //R分量 BYTE RED = GetRValue(cr); //G分量 BYTE GREEN = GetGValue(cr); //B分量 BYTE BLUE = GetBValue(cr); CString strText = _T(""); strText.Format(_T("COLORREF值:0x%08X"), cr); pDC->TextOut(100, 50, strText); strText.Format(_T("R分量:0x%02X"), RED); pDC->TextOut(100, 100, strText); strText.Format(_T("G分量:0x%02X"), GREEN); pDC->TextOut(100, 150, strText); strText.Format(_T("B分量:0x%02X"), BLUE); pDC->TextOut(100, 200, strText); }
16.14 如何給VARIANT類型賦值
void CDemoView::OnDraw(CDC* pDC) { VARIANT var; CString strText = _T(""); //初始化VARIANT類型變量 VariantInit(&var); //給VARIANT類型變量賦值 var.vt = VT_I4; var.lVal = (long)100; strText.Format(_T("var = %d"), var.lVal); pDC->TextOut(100, 50, strText); //清除VARIANT類型變量 VariantClear(&var); //給VARIANT類型變量賦值 var.vt = VT_R4; var.fltVal = 1.23f; strText.Format(_T("var = %f"), var.fltVal); pDC->TextOut(100, 100, strText); //改變VARIANT類型變量數(shù)據(jù)類型 VariantChangeType(&var, &var, 0, VT_R8); strText.Format(_T("var = %f"), var.dblVal); pDC->TextOut(100, 150, strText); }
16.15 如何將BYTE轉(zhuǎn)換成KB、MB和GB
void CDemoDlg::OnTest() { int nNum1 = GetDlgItemInt(IDC_NUM1); CString strNum2 = _T(""); //轉(zhuǎn)換成GB if (nNum1 > GB) { strNum2.Format(_T("%0.2fGB"), (double)nNum1 / GB); } //轉(zhuǎn)換成MB else if (nNum1 > MB) { strNum2.Format(_T("%0.2fMB"), (double)nNum1 / MB); } //轉(zhuǎn)換成KB else if (nNum1 > KB) { int n = nNum1 / KB; strNum2.Format(_T("%0.2fKB"), (double)nNum1 / KB); } else { strNum2.Format(_T("%dByte"), nNum1); } SetDlgItemText(IDC_NUM2, strNum2); } int i = 100;
long l = 2001;
float f=300.2;
double d=12345.119;
char username[]="程佩君";
char temp[200];
char *buf;
CString str;
_variant_t v1;
_bstr_t v2;
一、其它數(shù)據(jù)類型轉(zhuǎn)換為字符串短整型(int)
itoa(i,temp,10);///將i轉(zhuǎn)換為字符串放入temp中,最后一個(gè)數(shù)字表示十進(jìn)制
itoa(i,temp,2); ///按二進(jìn)制方式轉(zhuǎn)換 長(zhǎng)整型(long)
ltoa(l,temp,10); 浮點(diǎn)數(shù)(float,double)
用fcvt可以完成轉(zhuǎn)換,這是MSDN中的例子:
int decimal, sign;
char *buffer;
double source = 3.1415926535;
buffer = _fcvt( source, 7, &decimal, &sign );
運(yùn)行結(jié)果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0
decimal表示小數(shù)點(diǎn)的位置,sign表示符號(hào):0為正數(shù),1為負(fù)數(shù) CString變量
str = "2008北京奧運(yùn)";
buf = (LPSTR)(LPCTSTR)str; BSTR變量
BSTR bstrValue = ::SysAllocString(L"程序員");
char * buf = _com_util::ConvertBSTRToString(bstrValue);
SysFreeString(bstrValue);
AfxMessageBox(buf);
delete(buf); CComBSTR變量
CComBSTR bstrVar("test");
char *buf = _com_util::ConvertBSTRToString(bstrVar.m_str);
AfxMessageBox(buf);
delete(buf); _bstr_t變量
_bstr_t類型是對(duì)BSTR的封裝,因?yàn)橐呀?jīng)重載了=操作符,所以很容易使用
_bstr_t bstrVar("test");
const char *buf = bstrVar;///不要修改buf中的內(nèi)容
AfxMessageBox(buf); 通用方法(針對(duì)非COM數(shù)據(jù)類型)
用sprintf完成轉(zhuǎn)換
char buffer[200];
char c = '1';
int i = 35;
long j = 1000;
float f = 1.7320534f;
sprintf( buffer, "%c",c);
sprintf( buffer, "%d",i);
sprintf( buffer, "%d",j);
sprintf( buffer, "%f",f);
二、字符串轉(zhuǎn)換為其它數(shù)據(jù)類型
strcpy(temp,"123");
短整型(int)
i = atoi(temp); 長(zhǎng)整型(long)
l = atol(temp); 浮點(diǎn)(double)
d = atof(temp); CString變量
CString name = temp; BSTR變量
BSTR bstrValue = ::SysAllocString(L"程序員");
...///完成對(duì)bstrValue的使用
SysFreeString(bstrValue); CComBSTR變量
CComBSTR類型變量可以直接賦值
CComBSTR bstrVar1("test");
CComBSTR bstrVar2(temp); _bstr_t變量
_bstr_t類型的變量可以直接賦值
_bstr_t bstrVar1("test");
_bstr_t bstrVar2(temp);
三、其它數(shù)據(jù)類型轉(zhuǎn)換到CString
使用CString的成員函數(shù)Format來轉(zhuǎn)換,例如:
整數(shù)(int)
str.Format("%d",i); 浮點(diǎn)數(shù)(float)
str.Format("%f",i); 字符串指針(char *)等已經(jīng)被CString構(gòu)造函數(shù)支持的數(shù)據(jù)類型可以直接賦值
str = username; 對(duì)于Format所不支持的數(shù)據(jù)類型,可以通過上面所說的關(guān)于其它數(shù)據(jù)類型轉(zhuǎn)化到char *的方法先轉(zhuǎn)到char *,然后賦值給CString變量。
四、BSTR、_bstr_t與CComBSTR
CComBSTR 是ATL對(duì)BSTR的封裝,_bstr_t是C++對(duì)BSTR的封裝,BSTR是32位指針,但并不直接指向字串的緩沖區(qū)。
char *轉(zhuǎn)換到BSTR可以這樣:
BSTR b=_com_util::ConvertStringToBSTR("數(shù)據(jù)");///使用前需要加上comutil.h和comsupp.lib
SysFreeString(bstrValue);
反之可以使用
char *p=_com_util::ConvertBSTRToString(b);
delete p;
具體可以參考一,二段落里的具體說明。
CComBSTR與_bstr_t對(duì)大量的操作符進(jìn)行了重載,可以直接進(jìn)行=,!=,==等操作,所以使用非常方便。
特別是_bstr_t,建議大家使用它。
五、VARIANT 、_variant_t 與 COleVariant
VARIANT的結(jié)構(gòu)可以參考頭文件VC98\Include\OAIDL.H中關(guān)于結(jié)構(gòu)體tagVARIANT的定義。
對(duì)于VARIANT變量的賦值:首先給vt成員賦值,指明數(shù)據(jù)類型,再對(duì)聯(lián)合結(jié)構(gòu)中相同數(shù)據(jù)類型的變量賦值,舉個(gè)例子:
VARIANT va;
int a=2001;
va.vt=VT_I4;///指明整型數(shù)據(jù)
va.lVal=a; ///賦值
對(duì)于不馬上賦值的VARIANT,最好先用Void VariantInit(VARIANTARG FAR* pvarg);進(jìn)行初始化,其本質(zhì)是將vt設(shè)置為VT_EMPTY,下表我們列舉vt與常用數(shù)據(jù)的對(duì)應(yīng)關(guān)系:
Byte bVal;// VT_UI1.
Short iVal;// VT_I2.
long lVal;// VT_I4.
float fltVal;// VT_R4.
double dblVal;// VT_R8.
VARIANT_BOOL boolVal;// VT_BOOL.
SCODE scode;// VT_ERROR.
CY cyVal;// VT_CY.
DATE date;// VT_DATE.
BSTR bstrVal;// VT_BSTR.
DECIMAL FAR* pdecVal// VT_BYREF|VT_DECIMAL.
IUnknown FAR* punkVal;// VT_UNKNOWN.
IDispatch FAR* pdispVal;// VT_DISPATCH.
SAFEARRAY FAR* parray;// VT_ARRAY|*.
Byte FAR* pbVal;// VT_BYREF|VT_UI1.
short FAR* piVal;// VT_BYREF|VT_I2.
long FAR* plVal;// VT_BYREF|VT_I4.
float FAR* pfltVal;// VT_BYREF|VT_R4.
double FAR* pdblVal;// VT_BYREF|VT_R8.
VARIANT_BOOL FAR* pboolVal;// VT_BYREF|VT_BOOL.
SCODE FAR* pscode;// VT_BYREF|VT_ERROR.
CY FAR* pcyVal;// VT_BYREF|VT_CY.
DATE FAR* pdate;// VT_BYREF|VT_DATE.
BSTR FAR* pbstrVal;// VT_BYREF|VT_BSTR.
IUnknown FAR* FAR* ppunkVal;// VT_BYREF|VT_UNKNOWN.
IDispatch FAR* FAR* ppdispVal;// VT_BYREF|VT_DISPATCH.
SAFEARRAY FAR* FAR* pparray;// VT_ARRAY|*.
VARIANT FAR* pvarVal;// VT_BYREF|VT_VARIANT.
void FAR* byref;// Generic ByRef.
char cVal;// VT_I1.
unsigned short uiVal;// VT_UI2.
unsigned long ulVal;// VT_UI4.
int intVal;// VT_INT.
unsigned int uintVal;// VT_UINT.
char FAR * pcVal;// VT_BYREF|VT_I1.
unsigned short FAR * puiVal;// VT_BYREF|VT_UI2.
unsigned long FAR * pulVal;// VT_BYREF|VT_UI4.
int FAR * pintVal;// VT_BYREF|VT_INT.
unsigned int FAR * puintVal;//VT_BYREF|VT_UINT.
_variant_t是VARIANT的封裝類,其賦值可以使用強(qiáng)制類型轉(zhuǎn)換,其構(gòu)造函數(shù)會(huì)自動(dòng)處理這些數(shù)據(jù)類型。
使用時(shí)需加上#include <comdef.h>
例如:
long l=222;
ing i=100;
_variant_t lVal(l);
lVal = (long)i; COleVariant的使用與_variant_t的方法基本一樣,請(qǐng)參考如下例子:
COleVariant v3 = "字符串", v4 = (long)1999;
CString str =(BSTR)v3.pbstrVal;
long i = v4.lVal;
六、其它一些COM數(shù)據(jù)類型
根據(jù)ProgID得到CLSID
HRESULT CLSIDFromProgID( LPCOLESTR lpszProgID,LPCLSID pclsid);
CLSID clsid;
CLSIDFromProgID( L"MAPI.Folder",&clsid); 根據(jù)CLSID得到ProgID
WINOLEAPI ProgIDFromCLSID( REFCLSID clsid,LPOLESTR * lplpszProgID);
例如我們已經(jīng)定義了 CLSID_IApplication,下面的代碼得到ProgID
LPOLESTR pProgID = 0;
ProgIDFromCLSID( CLSID_IApplication,&pProgID);
...///可以使用pProgID
CoTaskMemFree(pProgID);//不要忘記釋放
七、ANSI與Unicode
Unicode稱為寬字符型字串,COM里使用的都是Unicode字符串。
將ANSI轉(zhuǎn)換到Unicode
(1)通過L這個(gè)宏來實(shí)現(xiàn),例如: CLSIDFromProgID( L"MAPI.Folder",&clsid);
(2)通過MultiByteToWideChar函數(shù)實(shí)現(xiàn)轉(zhuǎn)換,例如:
char *szProgID = "MAPI.Folder";
WCHAR szWideProgID[128];
CLSID clsid;
long lLen = MultiByteToWideChar(CP_ACP,0,szProgID,strlen(szProgID),szWideProgID,sizeof(szWideProgID));
szWideProgID[lLen] = '\0';
(3)通過A2W宏來實(shí)現(xiàn),例如:
USES_CONVERSION;
CLSIDFromProgID( A2W(szProgID),&clsid); 將Unicode轉(zhuǎn)換到ANSI
(1)使用WideCharToMultiByte,例如:
// 假設(shè)已經(jīng)有了一個(gè)Unicode 串 wszSomeString...
char szANSIString [MAX_PATH];
WideCharToMultiByte ( CP_ACP, WC_COMPOSITECHECK, wszSomeString, -1, szANSIString, sizeof(szANSIString), NULL, NULL );
(2)使用W2A宏來實(shí)現(xiàn),例如:
USES_CONVERSION;
pTemp=W2A(wszSomeString);
八、其它
對(duì)消息的處理中我們經(jīng)常需要將WPARAM或LPARAM等32位數(shù)據(jù)(DWORD)分解成兩個(gè)16位數(shù)據(jù)(WORD),例如:
LPARAM lParam;
WORD loValue = LOWORD(lParam);///取低16位
WORD hiValue = HIWORD(lParam);///取高16位 對(duì)于16位的數(shù)據(jù)(WORD)我們可以用同樣的方法分解成高低兩個(gè)8位數(shù)據(jù)(BYTE),例如:
WORD wValue;
BYTE loValue = LOBYTE(wValue);///取低8位
BYTE hiValue = HIBYTE(wValue);///取高8位 兩個(gè)16位數(shù)據(jù)(WORD)合成32位數(shù)據(jù)(DWORD,LRESULT,LPARAM,或WPARAM)
LONG MAKELONG( WORD wLow, WORD wHigh );
WPARAM MAKEWPARAM( WORD wLow, WORD wHigh );
LPARAM MAKELPARAM( WORD wLow, WORD wHigh );
LRESULT MAKELRESULT( WORD wLow, WORD wHigh ); 兩個(gè)8位的數(shù)據(jù)(BYTE)合成16位的數(shù)據(jù)(WORD)
WORD MAKEWORD( BYTE bLow, BYTE bHigh ); 從R(red),G(green),B(blue)三色得到COLORREF類型的顏色值
COLORREF RGB( BYTE byRed,BYTE byGreen,BYTE byBlue );
例如COLORREF bkcolor = RGB(0x22,0x98,0x34); 從COLORREF類型的顏色值得到RGB三個(gè)顏色值
BYTE Red = GetRValue(bkcolor); ///得到紅顏色
BYTE Green = GetGValue(bkcolor); ///得到綠顏色
BYTE Blue = GetBValue(bkcolor); ///得到蘭顏色
九、注意事項(xiàng)
假如需要使用到ConvertBSTRToString此類函數(shù),需要加上頭文件comutil.h,并在setting中加入comsupp.lib或者直接加上#pragma comment( lib, "comsupp.lib" )