-
-
-
- #if !defined(AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_)
- #define AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
-
-
-
-
-
-
-
- #define TS(value) CConvert::ToString(value)
- class CConvert
- {
-
- public:
- static CString ToString(BYTE btValue);
- static CString ToString(int iValue);
- static CString ToString(unsigned int iValue);
- static CString ToString(long lValue);
- static CString ToString(unsigned long lValue);
- static CString ToString(__int64 i64Value);
- static CString ToString(unsigned __int64 i64Value);
- static CString ToString(float fltValue);
- static CString ToString(double dblValue);
- static CString ToString(const COleDateTime& time);
- static CString ToString(const _variant_t& var);
- public:
- static BYTE ToByte(LPCTSTR lpszValue);
- static int ToInt(LPCTSTR lpszValue);
- static unsigned int ToUInt(LPCTSTR lpszValue);
- static long ToLong(LPCTSTR lpszValue);
- static unsigned long ToULong(LPCTSTR lpszValue);
- static __int64 ToInt64(LPCTSTR lpszValue);
- static unsigned __int64 ToUInt64(LPCTSTR lpszValue);
- static float ToFloat(LPCTSTR lpszValue);
- static double ToDouble(LPCTSTR lpszValue);
- static COleDateTime ToTime(LPCTSTR lpszValue);
- static _variant_t ToVariant(const COleDateTime& time);
- };
- extern CConvert Convert;
- #endif // !defined(AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_)
// Convert.h: interface for the CConvert class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_) #define AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ////////////////////////////////////////////////////////////////////// // Construction/Destruction /* * Copyright (c) 2010,zyq5945@126.com * All rights reserved. * * 文件名称:Convert.h * 文件标识:数据类型转换 * 摘 要:主要简化ADO中数据类型的转换 * * 当前版本:1.0 * 作 者:zyq5945@126.com(注意把中文字符换成英文字符) * 完成日期:2010年5月11日 * 发布Blog:http://blog.csdn.net/zyq5945/ * */ ////////////////////////////////////////////////////////////////////// #define TS(value) CConvert::ToString(value) class CConvert { public: static CString ToString(BYTE btValue); static CString ToString(int iValue); static CString ToString(unsigned int iValue); static CString ToString(long lValue); static CString ToString(unsigned long lValue); static CString ToString(__int64 i64Value); static CString ToString(unsigned __int64 i64Value); static CString ToString(float fltValue); static CString ToString(double dblValue); static CString ToString(const COleDateTime& time); static CString ToString(const _variant_t& var); public: static BYTE ToByte(LPCTSTR lpszValue); static int ToInt(LPCTSTR lpszValue); static unsigned int ToUInt(LPCTSTR lpszValue); static long ToLong(LPCTSTR lpszValue); static unsigned long ToULong(LPCTSTR lpszValue); static __int64 ToInt64(LPCTSTR lpszValue); static unsigned __int64 ToUInt64(LPCTSTR lpszValue); static float ToFloat(LPCTSTR lpszValue); static double ToDouble(LPCTSTR lpszValue); static COleDateTime ToTime(LPCTSTR lpszValue); static _variant_t ToVariant(const COleDateTime& time); }; extern CConvert Convert; #endif // !defined(AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_)
[cpp] view plain copy print ?
-
-
-
- #include "stdafx.h"
- #include "Convert.h"
- #include <comutil.h>
- #include <comdef.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
-
-
-
-
-
-
-
-
- CConvert Convert;
- CString CConvert::ToString(BYTE btValue)
- {
- CString strValue;
- strValue.Format(_T("%d"), btValue);
- return strValue;
- }
- CString CConvert::ToString(int iValue)
- {
- CString strValue;
- strValue.Format(_T("%d"), iValue);
- return strValue;
- }
- CString CConvert::ToString(unsigned int iValue)
- {
- CString strValue;
- strValue.Format(_T("%u"), iValue);
- return strValue;
- }
- CString CConvert::ToString(long lValue)
- {
- CString strValue;
- strValue.Format(_T("%d"), lValue);
- return strValue;
- }
- CString CConvert::ToString(unsigned long lValue)
- {
- CString strValue;
- strValue.Format(_T("%u"), lValue);
- return strValue;
- }
- CString CConvert::ToString(__int64 i64Value)
- {
- CString strValue;
- strValue.Format(_T("%I64d"), i64Value);
- return strValue;
- }
- CString CConvert::ToString(unsigned __int64 i64Value)
- {
- CString strValue;
- strValue.Format(_T("%I64u"), i64Value);
- return strValue;
- }
- CString CConvert::ToString(float fltValue)
- {
- CString strValue;
- strValue.Format(_T("%f"), fltValue);
- return strValue;
- }
- CString CConvert::ToString(double dblValue)
- {
- CString strValue;
- strValue.Format(_T("%f"), dblValue);
- return strValue;
- }
-
- CString CConvert::ToString(const COleDateTime& time)
- {
- CString strValue;
- strValue = time.Format(_T("%Y-%m-%d %H:%M:%S"));
- return strValue;
- }
- CString CConvert::ToString(const _variant_t& var)
- {
- CString strValue;
- switch (var.vt)
- {
- case VT_BSTR:
- case VT_LPSTR:
- case VT_LPWSTR:
- strValue = (LPCTSTR)(_bstr_t)var;
- break;
- case VT_I1:
- case VT_UI1:
- strValue.Format(_T("%d"), var.bVal);
- break;
- case VT_I2:
- strValue.Format(_T("%d"), var.iVal);
- break;
- case VT_UI2:
- strValue.Format(_T("%u"), var.uiVal);
- break;
- case VT_INT:
- strValue.Format(_T("%d"), var.intVal);
- break;
- case VT_I4:
- case VT_I8:
- strValue.Format(_T("%d"), var.lVal);
- break;
- case VT_UINT:
- strValue.Format(_T("%d"), var.uintVal);
- break;
- case VT_UI4:
- case VT_UI8:
- strValue.Format(_T("%d"), var.ulVal);
- break;
- case VT_VOID:
- strValue.Format(_T("%08x"), var.byref);
- break;
- case VT_R4:
- strValue.Format(_T("%f"), var.fltVal);
- break;
- case VT_R8:
- strValue.Format(_T("%f"), var.dblVal);
- break;
- case VT_DECIMAL:
- strValue.Format(_T("%f"), (double)var);
- break;
- case VT_CY:
- {
- COleCurrency cy = var.cyVal;
- strValue = cy.Format();
- }
- break;
- case VT_BLOB:
- case VT_BLOB_OBJECT:
- case 0x2011:
- strValue = _T("[BLOB]");
- break;
- case VT_BOOL:
- strValue = var.boolVal ? _T("TRUE") : _T("FALSE");
- break;
- case VT_DATE:
- {
- DATE dt = var.date;
- COleDateTime da = COleDateTime(dt);
- strValue = da.Format(_T("%Y-%m-%d %H:%M:%S"));
- }
- break;
- case VT_NULL:
- case VT_EMPTY:
- strValue = _T("");
- break;
- case VT_UNKNOWN:
- default:
- strValue = _T("VT_UNKNOW");
- break;
- }
-
- return strValue;
- }
- BYTE CConvert::ToByte(LPCTSTR lpszValue)
- {
- BYTE btValue;
- btValue = (BYTE)_ttoi(lpszValue);
- return btValue;
- }
- int CConvert::ToInt(LPCTSTR lpszValue)
- {
- int iValue;
- iValue = _ttoi(lpszValue);
- return iValue;
- }
- unsigned int CConvert::ToUInt(LPCTSTR lpszValue)
- {
- unsigned int iValue;
- iValue = _ttoi(lpszValue);
- return iValue;
- }
- long CConvert::ToLong(LPCTSTR lpszValue)
- {
- long lValue;
- lValue = _ttol(lpszValue);
- return lValue;
- }
- unsigned long CConvert::ToULong(LPCTSTR lpszValue)
- {
- unsigned long lValue;
- lValue = _ttol(lpszValue);
- return lValue;
- }
- __int64 CConvert::ToInt64(LPCTSTR lpszValue)
- {
- __int64 i64Value;
- i64Value = _ttoi64(lpszValue);
- return i64Value;
- }
- unsigned __int64 CConvert::ToUInt64(LPCTSTR lpszValue)
- {
- unsigned __int64 i64Value;
- i64Value = _ttoi64(lpszValue);
- return i64Value;
- }
- float CConvert::ToFloat(LPCTSTR lpszValue)
- {
- float fltValue;
- #ifdef _MBCS
- fltValue = (float)atof(lpszValue);
- #else
- fltValue = (float)wtof(lpszValue);
- #endif
- return fltValue;
- }
- double CConvert::ToDouble(LPCTSTR lpszValue)
- {
- double dblValue;
- #ifdef _MBCS
- dblValue = atof(lpszValue);
- #else
- dblValue = wtof(lpszValue);
- #endif
- return dblValue;
- }
-
-
-
-
-
- COleDateTime CConvert::ToTime(LPCTSTR lpszValue)
- {
- unsigned int iArray[6] = {2010, 5, 11, 12, 00, 00};
- int nIndex = 0;
- #if _MSC_VER >= 1310 //VC6.0不支持CStringAlt::Tokenize
- CString strTmp;
- int curPos = 0;
- CString strValue(lpszValue);
-
- strTmp = strValue.Tokenize(_T("- :"),curPos);
- while (strTmp != _T("") && nIndex <6)
- {
- iArray[nIndex++] = ToUInt(strTmp);
- strTmp = strValue.Tokenize(_T("- :"), curPos);
- };
- #else
- TCHAR tChar[MAX_PATH] = {0};
- TCHAR tCharTmp[MAX_PATH] = {0};
- TCHAR seps[] = "- :";
- TCHAR *next_token = NULL;
- ASSERT(_tcslen(lpszValue) < MAX_PATH);
- RtlCopyMemory(tChar, lpszValue, _tcslen(lpszValue) * sizeof(TCHAR));
- next_token = _tcstok(tChar, seps);
- while ( next_token != NULL && nIndex <6)
- {
- iArray[nIndex++] = _ttoi(next_token);
- next_token = _tcstok( NULL, seps);
- }
-
- #endif
- COleDateTime time(iArray[0], iArray[1], iArray[2],
- iArray[3], iArray[4], iArray[5]);
- return time;
- }
- _variant_t CConvert::ToVariant(const COleDateTime& time)
- {
- _variant_t vtVal;
-
- vtVal.vt = VT_DATE;
- vtVal.date = time;
- return vtVal;
- }
// Convert.cpp: implementation of the CConvert class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Convert.h" #include <comutil.h> #include <comdef.h> #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction /* * Copyright (c) 2010,zyq5945@126.com * All rights reserved. * * 文件名称:Convert.cpp * 文件标识:数据类型转换 * 摘 要:主要简化ADO中数据类型的转换 * * 当前版本:1.0 * 作 者:zyq5945@126.com(注意把中文字符换成英文字符) * 完成日期:2010年5月11日 * 发布Blog:http://blog.csdn.net/zyq5945/ * */ ////////////////////////////////////////////////////////////////////// CConvert Convert; CString CConvert::ToString(BYTE btValue) { CString strValue; strValue.Format(_T("%d"), btValue); return strValue; } CString CConvert::ToString(int iValue) { CString strValue; strValue.Format(_T("%d"), iValue); return strValue; } CString CConvert::ToString(unsigned int iValue) { CString strValue; strValue.Format(_T("%u"), iValue); return strValue; } CString CConvert::ToString(long lValue) { CString strValue; strValue.Format(_T("%d"), lValue); return strValue; } CString CConvert::ToString(unsigned long lValue) { CString strValue; strValue.Format(_T("%u"), lValue); return strValue; } CString CConvert::ToString(__int64 i64Value) { CString strValue; strValue.Format(_T("%I64d"), i64Value); return strValue; } CString CConvert::ToString(unsigned __int64 i64Value) { CString strValue; strValue.Format(_T("%I64u"), i64Value); return strValue; } CString CConvert::ToString(float fltValue) { CString strValue; strValue.Format(_T("%f"), fltValue); return strValue; } CString CConvert::ToString(double dblValue) { CString strValue; strValue.Format(_T("%f"), dblValue); return strValue; } // 时间输出格式:2010-05-06 22:07:08 CString CConvert::ToString(const COleDateTime& time) { CString strValue; strValue = time.Format(_T("%Y-%m-%d %H:%M:%S")); return strValue; } CString CConvert::ToString(const _variant_t& var) { CString strValue; switch (var.vt) { case VT_BSTR: //字符串 case VT_LPSTR: case VT_LPWSTR: strValue = (LPCTSTR)(_bstr_t)var; break; case VT_I1: //无符号字符 case VT_UI1: strValue.Format(_T("%d"), var.bVal); break; case VT_I2: //短整型 strValue.Format(_T("%d"), var.iVal); break; case VT_UI2: //无符号短整型 strValue.Format(_T("%u"), var.uiVal); break; case VT_INT: //整型 strValue.Format(_T("%d"), var.intVal); break; case VT_I4: //整型 case VT_I8: //长整型 strValue.Format(_T("%d"), var.lVal); break; case VT_UINT: //无符号整型 strValue.Format(_T("%d"), var.uintVal); break; case VT_UI4: //无符号整型 case VT_UI8: //无符号长整型 strValue.Format(_T("%d"), var.ulVal); break; case VT_VOID: strValue.Format(_T("%08x"), var.byref); break; case VT_R4: //浮点型 strValue.Format(_T("%f"), var.fltVal); break; case VT_R8: //双精度型 strValue.Format(_T("%f"), var.dblVal); break; case VT_DECIMAL: //小数 strValue.Format(_T("%f"), (double)var); break; case VT_CY: { COleCurrency cy = var.cyVal; strValue = cy.Format(); } break; case VT_BLOB: case VT_BLOB_OBJECT: case 0x2011: strValue = _T("[BLOB]"); break; case VT_BOOL: //布尔型 strValue = var.boolVal ? _T("TRUE") : _T("FALSE"); break; case VT_DATE: //日期型 { DATE dt = var.date; COleDateTime da = COleDateTime(dt); strValue = da.Format(_T("%Y-%m-%d %H:%M:%S")); } break; case VT_NULL://NULL值 case VT_EMPTY: //空 strValue = _T(""); break; case VT_UNKNOWN: //未知类型 default: strValue = _T("VT_UNKNOW"); break; } return strValue; } BYTE CConvert::ToByte(LPCTSTR lpszValue) { BYTE btValue; btValue = (BYTE)_ttoi(lpszValue); return btValue; } int CConvert::ToInt(LPCTSTR lpszValue) { int iValue; iValue = _ttoi(lpszValue); return iValue; } unsigned int CConvert::ToUInt(LPCTSTR lpszValue) { unsigned int iValue; iValue = _ttoi(lpszValue); return iValue; } long CConvert::ToLong(LPCTSTR lpszValue) { long lValue; lValue = _ttol(lpszValue); return lValue; } unsigned long CConvert::ToULong(LPCTSTR lpszValue) { unsigned long lValue; lValue = _ttol(lpszValue); return lValue; } __int64 CConvert::ToInt64(LPCTSTR lpszValue) { __int64 i64Value; i64Value = _ttoi64(lpszValue); return i64Value; } unsigned __int64 CConvert::ToUInt64(LPCTSTR lpszValue) { unsigned __int64 i64Value; i64Value = _ttoi64(lpszValue); return i64Value; } float CConvert::ToFloat(LPCTSTR lpszValue) { float fltValue; #ifdef _MBCS fltValue = (float)atof(lpszValue); #else fltValue = (float)wtof(lpszValue); #endif return fltValue; } double CConvert::ToDouble(LPCTSTR lpszValue) { double dblValue; #ifdef _MBCS dblValue = atof(lpszValue); #else dblValue = wtof(lpszValue); #endif return dblValue; } // 时间格式例子:2010-05-06 22:07:08 // 2010 05 06 22 07 08 // 2010:05:06 22:07:08 // 2010-05-06-22-07-08 // 只要是“- :”分割的时间格式都符合 COleDateTime CConvert::ToTime(LPCTSTR lpszValue) { unsigned int iArray[6] = {2010, 5, 11, 12, 00, 00}; int nIndex = 0; #if _MSC_VER >= 1310 //VC6.0不支持CStringAlt::Tokenize CString strTmp; int curPos = 0; CString strValue(lpszValue); strTmp = strValue.Tokenize(_T("- :"),curPos); while (strTmp != _T("") && nIndex <6) { iArray[nIndex++] = ToUInt(strTmp); strTmp = strValue.Tokenize(_T("- :"), curPos); }; #else TCHAR tChar[MAX_PATH] = {0}; TCHAR tCharTmp[MAX_PATH] = {0}; TCHAR seps[] = "- :"; TCHAR *next_token = NULL; ASSERT(_tcslen(lpszValue) < MAX_PATH); RtlCopyMemory(tChar, lpszValue, _tcslen(lpszValue) * sizeof(TCHAR)); next_token = _tcstok(tChar, seps); while ( next_token != NULL && nIndex <6) { iArray[nIndex++] = _ttoi(next_token); next_token = _tcstok( NULL, seps); } #endif COleDateTime time(iArray[0], iArray[1], iArray[2], iArray[3], iArray[4], iArray[5]); return time; } _variant_t CConvert::ToVariant(const COleDateTime& time) { _variant_t vtVal; vtVal.vt = VT_DATE; vtVal.date = time; return vtVal; }
由于_variant_t重载了以下等号运算符,所以下面类型可以直接给_variant_t赋值
[cpp] view plain copy print ?
- VARIANT
- VARIANT*
- _variant_t
- short
- long
- float
- double
- CY
- _bstr_t
- wchar_t
- char
- IDispatch*
- bool
- IUnknown*
- DECIMAL
- BYTE
- char
- unsigned short
- unsigned long
- int
- unsigned int
- __int64
- unsigned __int64
VARIANT VARIANT* _variant_t short long float double CY _bstr_t wchar_t char IDispatch* bool IUnknown* DECIMAL BYTE char unsigned short unsigned long int unsigned int __int64 unsigned __int64
由于COleDateTime重载了以下等号运算符,所以下面类型可以直接给COleDateTime赋值
[cpp] view plain copy print ?
- VARIANT
- DATE
- time_t
- __time64_t
- SYSTEMTIME
- FILETIME
- UDATE
VARIANT DATE time_t __time64_t SYSTEMTIME FILETIME UDATE
本例中CConvert类也增加了一个ToVariant函数把COleDateTime转换为_variant_t类型变量
[cpp] view plain copy print ?
- _variant_t varValue;
- CString strValue;
- COleDateTime dt;
- try
- {
-
- m_pRecordset->AddNew();
-
- GetDlgItemText(IDC_EDIT1, strValue);
- varValue = CConvert::ToByte(strValue);
- m_pRecordset->PutCollect("tByte", varValue);
-
-
- GetDlgItemText(IDC_EDIT2, strValue);
- varValue = (long)CConvert::ToInt(strValue);
- m_pRecordset->PutCollect("tInt", varValue);
-
- GetDlgItemText(IDC_EDIT3, strValue);
- varValue = CConvert::ToLong(strValue);
- m_pRecordset->PutCollect("tLong", varValue);
-
- GetDlgItemText(IDC_EDIT4, strValue);
- varValue = CConvert::ToFloat(strValue);
- m_pRecordset->PutCollect("tFloat", varValue);
-
- GetDlgItemText(IDC_EDIT5, strValue);
- varValue = CConvert::ToDouble(strValue);
- m_pRecordset->PutCollect("tDouble", varValue);
-
- GetDlgItemText(IDC_EDIT6, strValue);
- dt = CConvert::ToTime(strValue);
- varValue = CConvert::ToVariant(dt);
- m_pRecordset->PutCollect("tTime", varValue);
-
- GetDlgItemText(IDC_EDIT7, strValue);
- if (!strValue.CompareNoCase("TRUE"))
- {
- varValue = true;
- }
- else
- {
- varValue = false;
- }
- m_pRecordset->PutCollect("tBool", varValue);
-
- GetDlgItemText(IDC_EDIT8, strValue);
- varValue = strValue;
- m_pRecordset->PutCollect("tText", varValue);
- m_pRecordset->Update();
-
-
- int nCurSel = m_list.GetItemCount();
- ADOListRecord();
- SetListSel(nCurSel);
-
- AfxMessageBox("插入成功!");
- }
- catch(_com_error& e)
- {
- dump_com_error(e);
- }