|
-
January 30th, 2002, 06:04 AM
#1
Anyone knows how to do this
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
-
January 30th, 2002, 06:20 AM
#2
Re: Anyone knows how to do this
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
-
January 30th, 2002, 06:25 AM
#3
Re: Anyone knows how to do this
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
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 30th, 2002, 06:26 AM
#4
Re: Anyone knows how to do this
"istringstream", surely :-)
He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 30th, 2002, 07:37 AM
#5
Re: Anyone knows how to do this
yes of course 
The best things come to those who rate
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|