Click to See Complete Forum and Search --> : Anyone knows how to do this


Kohinoor24
January 30th, 2002, 05:04 AM
typedef std::string T_String;
typedef char T_8bit;
typedef short T_16bit;
typedef int T_32bit;

I have defined these typedefs and I want to convert the "T_string" type to short,char and int type.

I have a function like this:

void C_Ptx::Built_AMB(T_String value,bool ChainType)

where I need to convert the 1st parameter to short,char and int type.

How can I do it! please Help.

Kohinoor

NMTop40
January 30th, 2002, 05:20 AM
two options spring to mind:

For a char it is simple - the first character is your 'char'

For the others:

1. Use a C function such as strtol, but you'll need to call c_str() on the string. (No doubt the method Thew would suggest)

2. Create an ostringstream from the string and then use >>

You can do the same with double and also if you have wstring, in which case you use owstringstream instead.


The best things come to those who rate

Graham
January 30th, 2002, 05:25 AM
What do you mean by "convert"? If you mean you want to read a value coded in the string, then that's relatively simple:

#include <string>
#include <sstream>
// read an int from a string
std::string s = "12345";
std::istringstream iss(s);
int i;
iss >> i; // i now equals 12345



If, however, by "convert" you mean "cast", then forget it.

He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf

Graham
January 30th, 2002, 05:26 AM
"istringstream", surely :-)

He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf

NMTop40
January 30th, 2002, 06:37 AM
yes of course :)



The best things come to those who rate