ToString : convert types to string


V

Vincent RICHOMME

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.
 
Ad

Advertisements

M

Mike Wahler

Vincent RICHOMME said:
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();

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::eek:stringsream oss;
oss << i;
std::string s(oss.str());
// string now contains the sequence of characters:
'4', '2'

IF that 'wxString' type is from third party library,
it probably already includes stream operators.
BTW why are you using both 'std::string' and 'wxString'?
Why not choose one or the other?
Actually I am looking for intersting ways of doing this simple thing.

I'm typically more interested in keeping my code simple (thus
more readable/maintainable). 'Interesting' often becomes
'unnecessarily complex'.

-Mike
 
Ad

Advertisements

I

Ian Collins

Mike said:
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::eek:stringsream 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::eek:stringstream 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.
 

Top