在C++中将字符串类转换为整型,浮点型并不像java,C#那样简单,这是件烦心的工作,而且不同的函数接口让代码维护起来也麻烦,所以写个自动进行字符串转换成所需要的类型的程序很有意义,下面这个类只有加入你想要的类型,并为之提供操作符重载就可以了。
注:如果你没有使用boost库,把#define USE_BOOST_LIBS注释掉
/**
* @brief Class that make string data automaically converted to numeric data
*
*/
class CStringAutoCast
{
public:
/**
* E_format: enumeration of type of input data
*/
typedef enum
{
kDec = 0,
kBin = 1,
kHex = 2,
kOct = 3
}E_format;
public:
CStringAutoCast(const std::string & input, E_format format = kDec);
virtual ~CStringAutoCast();
operator bool() const;
operator std::string() const;
operator unsigned int() const;
operator int() const;
operator unsigned short() const;
operator short() const;
operator unsigned long() const;
operator long() const;
operator unsigned char() const;
operator char() const;
operator float() const;
protected:
/**
* m_data: data to convert
*/
std::string m_data;
/**
* m_format: format of data to convert from
*/
E_format m_format;
};
#include "stdafx.h"
#include "StringAutoCast.h"
//comment this line if you cannot compile with boost librairies
#define USE_BOOST_LIBS
#ifdef USE_BOOST_LIBS
#include "boost/lexical_cast.hpp"
#endif
//////////////////////////////////////////////////////////////////////
// Global functions
//////////////////////////////////////////////////////////////////////
/**
* @brief convert a string from a E_format (hex, dec, oct or bin) to an integer-like return type
*
* @param m_data string data to convert
* @param m_format format of data
*
* @return data converted to template type
*/
template <class T>
T _internal_integer_convert(const std::string & m_data, const CStringAutoCast::E_format m_format)
{
T ret;
switch(m_format)
{
case CStringAutoCast::kDec:
sscanf(m_data.c_str(), "%d", &ret);
break;
case CStringAutoCast::kBin:
sscanf(m_data.c_str(), "%b", &ret);
break;
case CStringAutoCast::kHex:
sscanf(m_data.c_str(), "%x", &ret);
break;
case CStringAutoCast::kOct:
sscanf(m_data.c_str(), "%o", &ret);
break;
}
return ret;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/**
* @brief Constructor
*
* @param input data to convert
* @param format format of data
*
*/
CStringAutoCast::CStringAutoCast(const std::string & input, E_format format/*=kDec*/)
:m_format(format),m_data(input)
{
}
/**
* @brief Destructor
*
*
*/
CStringAutoCast::~CStringAutoCast()
{
}
/**
* @brief operator for bool
*
*
* @return CStringAutoCast data as bool
*/
CStringAutoCast::operator bool() const
{//这里就不去考虑大小写的问题了
return (m_data=="true" || m_data=="1");
}
/**
* @brief operator for std::string
*
*
* @return CStringAutoCast data as std::string (no conversion in this case)
*/
CStringAutoCast::operator std::string() const
{
return m_data;
}
/**
* @brief operator for unsigned int
*
*
* @return CStringAutoCast data as unsigned int
*/
CStringAutoCast::operator unsigned int() const
{
return _internal_integer_convert<unsigned int>(m_data, m_format);
}
/**
* @brief operator for int
*
*
* @return CStringAutoCast data as int
*/
CStringAutoCast::operator int() const
{
return _internal_integer_convert<int>(m_data, m_format);
}
/**
* @brief operator for unsigned short
*
*
* @return CStringAutoCast data as unsigned short
*/
CStringAutoCast::operator unsigned short() const
{
return _internal_integer_convert<unsigned short>(m_data, m_format);
}
/**
* @brief operator for short
*
*
* @return CStringAutoCast data as short
*/
CStringAutoCast::operator short() const
{
return _internal_integer_convert<short>(m_data, m_format);
}
/**
* @brief operator for unsigned long
*
*
* @return CStringAutoCast data as unsigned long
*/
CStringAutoCast::operator unsigned long() const
{
return _internal_integer_convert<unsigned long>(m_data, m_format);
}
/**
* @brief operator for long
*
*
* @return CStringAutoCast data as long
*/
CStringAutoCast::operator long() const
{
return _internal_integer_convert<long>(m_data, m_format);
}
/**
* @brief operator for unsigned char
*
*
* @return CStringAutoCast data as unsigned char
*/
CStringAutoCast::operator unsigned char() const
{
return _internal_integer_convert<unsigned char>(m_data, m_format);
}
/**
* @brief operator for char
*
*
* @return CStringAutoCast data as char
*/
CStringAutoCast::operator char() const
{
return _internal_integer_convert<char>(m_data, m_format);
}
/**
* @brief operator for float
*
*
* @return CStringAutoCast data as float
*/
CStringAutoCast::operator float() const
{
#ifdef USE_BOOST_LIBS
return boost::lexical_cast<float>(m_data);
#else
float ret;
sscanf(m_data.c_str(), "%f", &ret);
return ret;
#endif
}
// StringAutomaticCast.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <string>
#include "StringAutoCast.h"
/**
* @brief Read data from a file and automatically convert it to return data type
*
* @param format specify the format of read data (only for inter-like data (int, short, char, long,)
*
* @return CStringAutoCast
*/
CStringAutoCast ReadDataFromFile(CStringAutoCast::E_format format= CStringAutoCast::kDec)
{
/*
here put some code that read a file (txt, xml or whatever you want) and
extract data as std::string csReadFromFile
*/
const std::string csReadFromFile = "230";
return CStringAutoCast(csReadFromFile, format);
}
int main(int argc, char* argv[])
{
int a = ReadDataFromFile();
float b = ReadDataFromFile();
unsigned char c = ReadDataFromFile();
long d = ReadDataFromFile(CStringAutoCast::kOct); //specifies that the string read is in Octal format
unsigned long e = ReadDataFromFile(CStringAutoCast::kHex); //specifies that the string read is in Hexadecimal format
std::string f = ReadDataFromFile();
return 0;
}