代码改变世界

Visual C++ 基础数据类型的转换

2011-04-12 10:10  Clingingboy  阅读(3062)  评论(0编辑  收藏  举报

 

16.1如何将基本数据类型转换成CString类型

用CString的Format方法

void CDemoView::OnDraw(CDC* pDC)
{
    int a = 100;
    double b = 1.23;
    //将整型转换成CString
    CString str1 = _T("");
    str1.Format(_T("%d"), a);
    //将实型转换成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类型转换成基本数据类型

atoi:Convert a string to integer.
参考:http://baike.baidu.com/view/653935.htm

void CDemoView::OnDraw(CDC* pDC)
{
    CString str1 = _T("100");
    CString str2 = _T("1.23");
    //将CString转换成整型
    int a = atoi(str1);
    //将CString转换成实型
    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类型转换成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_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类型转换成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("");
    //转换成GB
    if (nNum1 > GB)
    {
        strNum2.Format(_T("%0.2fGB"), (double)nNum1 / GB);
    }
    //转换成MB
    else if (nNum1 > MB)
    {
        strNum2.Format(_T("%0.2fMB"), (double)nNum1 / MB);
    }
    //转换成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);
}