在工程项目中是否常常需要将整型变量转换成字符串变量呢,或是将字符串变量转换成整型变量呢?
wuxfei@gmail.com。
接口声明:
/********************************************************************
created: 21:1:2013 16:54
filename: MyInt64.h
author: 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 > 1000
class 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:54
filename: MyInt64.cpp
author: 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。