Visual C++ 基础数据类型的转换
生活随笔
收集整理的這篇文章主要介紹了
Visual C++ 基础数据类型的转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
16.1如何將基本數據類型轉換成CString類型
用CString的Format方法
void CDemoView::OnDraw(CDC* pDC) {int a = 100;double b = 1.23;//將整型轉換成CStringCString str1 = _T("");str1.Format(_T("%d"), a);//將實型轉換成CStringCString 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類型轉換成基本數據類型
atoi:Convert a string to integer.?
參考:http://baike.baidu.com/view/653935.htm
?
16.3如何將TCHAR類型轉換成CString類型
void CDemoView::OnDraw(CDC* pDC) {TCHAR sz[] = _T("Hello world!");//直接賦值CString str1 = sz;//調用CString::Format函數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類型轉換成TCHAR類型
?
void CDemoView::OnDraw(CDC* pDC) {CString str = _T("Hello world!");//強制轉換LPTSTR psz1 = (LPTSTR)(LPCTSTR)str;//調用CString::GetBuffer函數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類型轉換成BSTR類型
?
void CDemoView::OnDraw(CDC* pDC) {TCHAR sz[] = _T("Hello world!");//調用ConvertStringToBSTR函數BSTR bstr1 = _com_util::ConvertStringToBSTR(sz);//使用_bstr_tBSTR 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類型轉換成TCHAR類型
void CDemoView::OnDraw(CDC* pDC) {BSTR bstr = L"Hello world!";//調用ConvertBSTRToString函數LPTSTR psz = _com_util::ConvertBSTRToString(bstr);CString strText = _T("");strText.Format(_T("psz = %s"), psz);pDC->TextOut(100, 50, strText); }?
16.7 如何將BSTR類型轉換成CString類型
SysAllocString和SysFreeString?
void CDemoView::OnDraw(CDC* pDC) {BSTR bstr = ::SysAllocString(L"Hello world!");//強制轉換CString str = (CString)bstr;CString strText = _T("");strText.Format(_T("str = %s"), str);pDC->TextOut(100, 50, strText);::SysFreeString(bstr); }?
16.8如何將CString類型轉換成BSTR類型
void CDemoView::OnDraw(CDC* pDC) {CString str = _T("Hello world!");//調用CString::AllocSysString函數BSTR bstr = str.AllocSysString();CString strText = _T("");strText.Format(_T("bstr = %s"), (CString)bstr);pDC->TextOut(100, 50, strText);::SysAllocString(bstr); }?
16.9 如何將DWORD類型轉換成WORD類型
LOWORD和HIWORD
void CDemoView::OnDraw(CDC* pDC) {//將1個DWORD類型數據分解成2個WORD類型數據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類型轉換成BYTE類型
LOBYTE和HIBYTE?
void CDemoView::OnDraw(CDC* pDC) {//將1個WORD類型數據分解成2個BYTE類型數據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個WORD類型數據組合成1個DWORD類型數據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類型轉換成WORD類型
?
void CDemoView::OnDraw(CDC* pDC) {//將2個BYTE類型數據組合成1個WORD類型數據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類型轉換成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類型變量數據類型VariantChangeType(&var, &var, 0, VT_R8);strText.Format(_T("var = %f"), var.dblVal);pDC->TextOut(100, 150, strText); }16.15 如何將BYTE轉換成KB、MB和GB
?
void CDemoDlg::OnTest() {int nNum1 = GetDlgItemInt(IDC_NUM1);CString strNum2 = _T("");//轉換成GBif (nNum1 > GB){strNum2.Format(_T("%0.2fGB"), (double)nNum1 / GB);}//轉換成MBelse if (nNum1 > MB){strNum2.Format(_T("%0.2fMB"), (double)nNum1 / MB);}//轉換成KBelse 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); }轉載于:https://www.cnblogs.com/jack-jia-moonew/p/4261843.html
總結
以上是生活随笔為你收集整理的Visual C++ 基础数据类型的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZRender源码分析2:Storage
- 下一篇: u-boot,linux,文件系统移植笔