M
ma740988
The string object value_f doesn't produce the right output. At issue,
- I suspect - is the conversion from string to int with istringstream.
An alternate approach? Thanks in advance
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Conversion Functions
template<typename T>
T fromString(const std::string& s) {
std::istringstream is(s);
T t;
is >> t;
return t;
}
template<typename T>
std::string toString(const T& t) {
std:stringstream s;
s << t;
return s.str();
}
//
// test function to convert values:
// 0xAFFF from string to int
// 0xAFFF from int to string
int main()
{
int d(0xAFFF);
string value_d = toString( d );
cout << "value_d is [" << value_d << "]" << endl;
string value_e( " 4999 " );
string value_f( " 0xAFFF " );
int val_1 = fromString<int>( value_e );
cout << "val_1 is [" << val_1 << "]" << endl;
int val_2 = fromString<int>( value_f );
cout << "val_2 is [" << val_2 << "]" << std::hex << endl;
}
- I suspect - is the conversion from string to int with istringstream.
An alternate approach? Thanks in advance
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Conversion Functions
template<typename T>
T fromString(const std::string& s) {
std::istringstream is(s);
T t;
is >> t;
return t;
}
template<typename T>
std::string toString(const T& t) {
std:stringstream s;
s << t;
return s.str();
}
//
// test function to convert values:
// 0xAFFF from string to int
// 0xAFFF from int to string
int main()
{
int d(0xAFFF);
string value_d = toString( d );
cout << "value_d is [" << value_d << "]" << endl;
string value_e( " 4999 " );
string value_f( " 0xAFFF " );
int val_1 = fromString<int>( value_e );
cout << "val_1 is [" << val_1 << "]" << endl;
int val_2 = fromString<int>( value_f );
cout << "val_2 is [" << val_2 << "]" << std::hex << endl;
}