Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1032279
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类: C/C++

2011-07-04 23:07:37

#include 
#include 
#include 

//---------------------------------------------------------------------------
// Convert a number to a string

template<class T>
std::string ToStr(const T &value)
{
  std::ostringstream oss;
  if(!(oss<throw exception("Invalid argument");
  return oss.str();
}


//---------------------------------------------------------------------------
// Convert a string to a number

template<class T>
T ToInt(const std::string &str)
{
  if(str.size()==0)return 0;
  std::istringstream iss(str);
  T result=0;
  if(!(iss>>std::dec>>result))throw exception("Invalid argument");
  return result;
}


//---------------------------------------------------------------------------
// Convert an hex string to a number

template<class T>
T HexToInt(const std::string &str)
{
  if(str.size()==0)return 0;
  std::istringstream iss(str);
  T result=0;
  if(!(iss>>std::hex>>result))throw exception("Invalid argument");
  return result;
}


//---------------------------------------------------------------------------
// Convert a number to an hex string

template<class T>
std::string ToHex(const T &value)
{
  std::ostringstream oss;
  if(!(oss<throw exception("Invalid argument");
  return oss.str();
}
阅读(1471) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~