标签:png bsp 构造函数 wchar_t aci variant ola mem ***
编程中需要将_variant_t转换为char*,常用的方法是:(const char*)_bstr_t(c_variant_t);
使用_bstr_t的构造函数: _bstr_t(const _variant_t& var) ;
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Construct a _bstr_t from a const _variant_t&
// inline _bstr_t::_bstr_t(const _variant_t &var) : m_Data(NULL) { if (V_VT(&var) == VT_BSTR) { *this = V_BSTR(&var); return; } _variant_t varDest; varDest.ChangeType(VT_BSTR, &var); *this = V_BSTR(&varDest); } |
但是在转换小于1(如0.02)的float、double类型时会造成整数部分的0丢失的问题,详细的代码以及可行解决方案如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/********************************************************************
created: 2018/07/20 20:7:2018 15:21 file base: _bstr_t file ext: cpp author: Michael Joessy purpose: _bstr_t *********************************************************************/ #include <iostream> #include <string> #include <comutil.h> using namespace std; #pragma comment(lib, "comsupp.lib") int main() { char szStr[16]; _variant_t _varR4; _varR4.vt = VT_R4; _varR4.fltVal = 0.021; cout << "转换前:\t" << _varR4.fltVal << endl; _bstr_t _bstr4 = _bstr_t(_varR4); wcout << "转换后:\t" << (wchar_t*)_bstr4 << endl; //丢失整数部分的0 memset(szStr, 0, 16); sprintf(szStr, "%10.6f", (double)_varR4.fltVal); cout << "转换Ex:\t" << szStr << endl; _variant_t _varR8; _varR8.vt = VT_R8; _varR8.dblVal = 0.023; cout << "转换前:\t" << _varR8.dblVal << endl; _bstr_t _bstr8 = _bstr_t(_varR8); wcout << "转换后:\t" << (wchar_t*)_bstr8 << endl; //丢失整数部分的0 memset(szStr, 0, 16); sprintf(szStr, "%10.6f", _varR8.dblVal); cout << "转换Ex:\t" << szStr << endl; // 等待键盘输入... cin.get(); return 0; } |
标签:png bsp 构造函数 wchar_t aci variant ola mem ***
原文地址:https://www.cnblogs.com/MakeView660/p/9341939.html