473,503 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Jan 27 '06 #1
2 7213

"Vincent RICHOMME" <ri******@free.fr> wrote in message
news:43*********************@news.free.fr...
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::ostringsream 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
Jan 27 '06 #2
Mike Wahler wrote:

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.
Jan 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2852
by: Chris | last post by:
HI, what is the purpose of XmlConvert.ToString() ? why not just use Convert.ToString() ? e.g. Int32 vInt32 = -2147483648; string s1 = XmlConvert.ToString(vInt32); string s2 =...
8
4073
by: Ahjay Muscha | last post by:
What's the best way to convert from a double to a string? for example, at the moment I have to do this: string s = new Double(a + b / c).ToString(); Is there a better way for me to do this...
3
21394
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
5
5039
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
101
19101
by: Sean | last post by:
Book I am reading says that Cstr() is best method for efficency and safety however it doesnt compare that method of the .ToString() method. Which is best. Thanks
4
4861
by: Larry Smith | last post by:
Once you call "ToString()" for an object, how can you then convert the string back to the original object (assuming you know its type of course)? Also, why does "ToString()" produce one string and...
3
1946
by: Mike S | last post by:
Maybe I'm missing some fundamental unwritten law of OOP, but I was wondering why the VB.NET compiler doesn't take advantage of the fact that all .NET objects, being derived from the Object base...
6
21778
by: Bob | last post by:
Hello folks. I use this formating but if the value in dr is a null it generates an error "Object cannot be cast from DBNull to other types." Anybody know how I can deal with nulls? ...
11
28037
by: Gary James | last post by:
I'm using an object data type variable to pass a numeric value (Int, Float, Double, etc) to a function that returns a formatted string. However, nullable types do not provide an overridden...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.