整型变量 与 字符串变量

来源:互联网 发布:xampp1.7.3sql注入 编辑:程序博客网 时间:2024/06/05 08:27
在工程项目中是否常常需要将整型变量转换成字符串变量呢,或是将字符串变量转换成整型变量呢?

wuxfei@gmail.com。


接口声明:

/********************************************************************created:21:1:2013   16:54filename: MyInt64.hauthor:wuxfei@gmail.com*********************************************************************/#if !defined(AFX_MYINT64_H__B544531C_83B8_4110_8D36_408EABB7A7B3__INCLUDED_)#define AFX_MYINT64_H__B544531C_83B8_4110_8D36_408EABB7A7B3__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CMyInt64  {public:CMyInt64(BOOL bSign=TRUE);virtual ~CMyInt64();public://赋值void operator=(INT64 nNew);void operator=(UINT64 nNew);void operator=(CString str);//当字符串使用operator LPCTSTR();private:BOOL m_bSign;//是否为有符号INT64 m_Int64;//有符号存储UINT64 m_uInt64;//无符号存储CString m_str;};#endif // !defined(AFX_MYINT64_H__B544531C_83B8_4110_8D36_408EABB7A7B3__INCLUDED_)

源码实现:

/********************************************************************created:21:1:2013   16:54filename: MyInt64.cppauthor:wuxfei@gmail.com*********************************************************************/#include "stdafx.h"#include "MyInt64.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction///////////////////////////////////////////////////////////////////////************************************************************************\* 构造函数          \************************************************************************/CMyInt64::CMyInt64(BOOL bSign){m_bSign = bSign;}/************************************************************************\* 析构          \************************************************************************/CMyInt64::~CMyInt64(){}/************************************************************************\* 赋值操作          \************************************************************************/void CMyInt64::operator =(INT64 nNew){m_Int64 = nNew;m_uInt64 = nNew;}/************************************************************************\* 赋值操作          \************************************************************************/void CMyInt64::operator =(UINT64 nNew){m_Int64 = nNew;m_uInt64 = nNew;}/************************************************************************\* 赋值操作          \************************************************************************/void CMyInt64::operator =(CString str){_stscanf((LPCTSTR)str, _T("%I64d"), &m_Int64);_stscanf((LPCTSTR)str, _T("%I64u"), &m_uInt64);}/************************************************************************\* 当做字符串使用          \************************************************************************/CMyInt64::operator LPCTSTR(){if (m_bSign){m_str.Format(_T("%I64d"), m_Int64);}else{m_str.Format(_T("%I64u"), m_uInt64);}return (LPCTSTR)m_str;}

如何使用:

CMyInt64 n(FALSE);n = _T("18446744073709551614");_tprintf(_T("%s"), (LPCTSTR)n);

小弟能力有限,欢迎指点,wuxfei@gmail.com。

原创粉丝点击