 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Vincent RICHOMME Guest
|
Posted: Fri Jan 27, 2006 9:32 pm Post subject: ToString : convert types to string |
|
|
Hi,
I am developping a software to test smartcard and one of my class is
shown below :
class SCardCmd
{
public:
SCardCmd(const wxString& strCardName, const wxString& strCmdName):
m_strCardName(strCardName),
m_strCmdName(strCmdName),
m_CLA(0),
m_INS(0),
m_P1(0),
m_P2(0),
m_Lc(0),
m_Data(0),
m_Le(0),
m_Resp(0),
m_SW(0){}
const wxString GetCmdName() { return m_strCmdName; }
const wxString GetCardName() { return m_strCardName; }
private:
wxString m_strCardName;
wxString m_strCmdName;
u8 m_CLA;
u8 m_INS;
u8 m_P1;
u8 m_P2;
u8 m_Lc;
u8 m_Data;
u8 m_Le;
u8 m_Resp;
u16 m_SW;
};
then I want to display this information but my problem is to convert all
my values to string.
What is the better solution :
1)write a class with static methods to convert types :
ex
class StrConv
{
static std::string StrConv(u8 value) {...;}
static std::string StrConv(u16 value) {...;}
};
2)define object type something like :
struct U8
{
u8 value;
std::string toString();
};
or a template class that would allow me to write
Type<u8> mytype;
Type<u16> mytype2;
std::sring string = mytype.tostring();
Actually I am looking for intersting ways of doing this simple thing. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sat Jan 28, 2006 1:15 am Post subject: Re: ToString : convert types to string |
|
|
Mike Wahler wrote:
| Quote: |
You only need do any of that if there are already no
stream operators defined for those types (at least
some of which I suspect are typedefs for native types)
If they are, just use an ostringstream;
int i(2);
std::ostringsream oss;
oss << i;
std::string s(oss.str());
// string now contains the sequence of characters:
'4', '2'
The biggest problem with this approach is that the standard streaming |
operators don't give you an output of char types that is consistent with
other integer types, which are used in his example (assuming u8 is a
typedef to unsigned char).
A simple conversion template could be an answer, especially if the
output is to be formatted in some way,for example
template <typename T> std::string
toString( T value )
{
std::ostringstream ss;
ss << value;
return ss.str();
}
template <> std::string
toString( uint8_t value )
{
unsigned n = value;
return toString( n );
}
While over the top for a simple case, would be handy if something other
than the value was to be output.
--
Ian Collins. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|