VC++ 15个数据类型转换的示例代码

本文详细介绍多种数据类型之间的转换方法,包括基本数据类型、字符串类型、复合数据类型等,并提供了具体的代码示例,帮助读者理解和掌握不同类型间的转换。

如何给VARIANT类型赋值

{
	VARIANT var;
	CString strText = _T("");

	//初始化VARIANT类型变量
	VariantInit(&var);

	//给VARIANT类型变量赋值
	var.vt = VT_I4;
	var.lVal = (long)100;
	strText.Format(_T("var = %d"), var.lVal);
	MessageBox(strText);

	//清除VARIANT类型变量
	VariantClear(&var);

	//给VARIANT类型变量赋值
	var.vt = VT_R4;
	var.fltVal = 1.23f;
	strText.Format(_T("var = %f"), var.fltVal);
	MessageBox(strText);

	//改变VARIANT类型变量数据类型
	VariantChangeType(&var, &var, 0, VT_R8);
	strText.Format(_T("var = %f"), var.dblVal);
	MessageBox(strText);
}

如何将BSTR类型转换成CString类型

{
	BSTR bstr = ::SysAllocString(L"Hello world!");

	//强制转换
	CString str = (CString)bstr;

	CString strText = _T("");
	strText.Format(_T("str = %s"), str);
	MessageBox(strText);

	::SysFreeString(bstr);
}

如何将BSTR类型转换成TCHAR类型

{
	BSTR bstr = L"Hello world!";

	//调用ConvertBSTRToString函数
	LPTSTR psz = _com_util::ConvertBSTRToString(bstr);
	CString strText = _T("");
	strText.Format(_T("psz = %s"), psz);
	MessageBox(strText);
}

如何将BYTE类型转换成WORD类型

{
	//将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);
	MessageBox(strText);

	strText.Format(_T("high-order byte:0x%02X"), bHigh);
	MessageBox(strText);

	strText.Format(_T("WORD:0x%04X"), wValue);
	MessageBox(strText);
}


如何将BYTE转换成KB、MB和GB


如何将COLORREF类型转换成RGB分量

{
	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);
	MessageBox(strText);

	strText.Format(_T("R分量:0x%02X"), RED);
	MessageBox(strText);

	strText.Format(_T("G分量:0x%02X"), GREEN);
	MessageBox(strText);

	strText.Format(_T("B分量:0x%02X"), BLUE);
	MessageBox(strText);
}

如何将CString类型转换成BSTR类型

{
	CString str = _T("Hello world!");

	//调用CString::AllocSysString函数
	BSTR bstr = str.AllocSysString();

	CString strText = _T("");
	strText.Format(_T("bstr = %s"), (CString)bstr);
	MessageBox(strText);
 
	::SysAllocString(bstr);
}

如何将CString类型转换成TCHAR类型

{
	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);
	MessageBox(strText);

	strText.Format(_T("psz2 = %s"), psz2);
	MessageBox(strText);
}

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

{
	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);
	MessageBox(strText);

	strText.Format(_T("b = %f"), b);
	MessageBox(strText);
}

如何将DWORD类型转换成WORD类型

{
	//将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);
	MessageBox(strText);

	strText.Format(_T("low-order word:0x%04X, high-order word:0x%04X"), wLow, wHigh);
	MessageBox(strText);
}

如何将TCHAR类型转换成BSTR类型

{
	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);
	MessageBox(strText);

	strText.Format(_T("bstr2 = %s"), (CString)bstr2);
	MessageBox(strText);
}

如何将TCHAR类型转换成CString类型


如何将WORD类型转换成BYTE类型

{
	//将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);
	MessageBox(strText);

	strText.Format(_T("low-order byte:0x%02X"), bLow);
	MessageBox(strText);

	strText.Format(_T("high-order byte:0x%02X"), bHigh);
	MessageBox(strText);
}

如何将WORD类型组合成DWORD类型

{
	//将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);
	MessageBox(strText);

	strText.Format(_T("high-order word:0x%04X"), wHigh);
	MessageBox(strText);

	strText.Format(_T("DWORD:0x%08X"), dwValue);
	MessageBox(strText);
}

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


实例2-1:演示C++程序的运行步骤。源代码在光盘中“\02\firstcpp”目录下。 实例2-2:Visual C++语言基本元素介绍。源代码在光盘中“\02\secondcpp”目录下。 实例2-3:cout流控制符setw的使用。源代码在光盘中“\02\thirdcpp”目录下。 实例2-4:增加域内填充字符。源代码在光盘中“\02\fourthcpp”目录下。 实例2-5:同一数据用不同进制输出结果。源代码在光盘中“\02\fifthcpp”目录下。 实例2-6:用浮点数的形式输出数据。源代码在光盘中“\02\sixthcpp”目录下。 实例2-7:数据的输入及输出。源代码在光盘中“\02\seventhcpp”目录下。 实例2-8:计算学生平均成绩。源代码在光盘中“\02\eighthcpp”目录下。 实例2-9:函数的调用。源代码在光盘中“\02\ninthcpp”目录下。 实例2-10:用指针传递参数。源代码在光盘中“\02\tenthcpp”目录下。 实例2-11:全局变量和局部变量。源代码在光盘中“\02\eleventhcpp”目录下。 实例2-12:使用指针注意内存的分配。源代码在光盘中“\02\twelfthcpp”目录下。 实例2-13:引用实例。源代码在光盘中“\02\thirteencpp”目录下。 实例2-14:虚函数的使用实例。源代码在光盘中“\02\fourteencpp”目录下。 。。。。。。 实例13-1:动态链接库(Dll)实例。源代码在光盘中“\13\dlltest ”目录下。 实例13-2:鼠标钩子应用实例。源代码在光盘中“\13\MouseHook”目录下。 实例13-3:键盘钩子应用实例。源代码在光盘中“\13\KeyboardHook”目录下。 实例14-1:WinSock网络程序:聊天室。源代码在光盘中“\14\WinSocketChat”目录下。 实例14-2:利用MSComm控件进行串口数据传输。源代码在光盘中“\14\MSComm”目录下。 实例14-3:简单的WinInet应用程序。源代码在光盘中“\14\SimpleNet”目录下。 实例15-1:创建Access数据库。源代码在光盘的“\15\Info”中。 实例15-2:MFC的ODBC数据库实例。源代码在光盘中“\15\Enroll”目录下。 实例15-3:利用ADO查询并操作数据库实例。源代码在光盘中“\15\AdoTest”目录下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hellokandy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值