Using std::stringstream to convert a char to int

V

Vijai Kalyan

I wrote the following function as a curiosity:

template<typename SourceType,
typename DestinationType>
DestinationType NumericCast(const SourceType& value) {
std::wstringstream strbuf;
strbuf << value << std::endl ;
strbuf.flush();
DestinationType convalue;
strbuf >> convalue;
return convalue;
}

This works fine for all numeric types such as int to long, long to
float etc.

However, I tried the following:

NumericCast<char, int>('c');

This failed to return the correct value. When I stepped through the
function, the value of "convalue" remained the same throughout. But
this happened only when SourceType was char. A

I tried stepping into the operator code, but my debugger just stepped
over so I am not sure what's going on inside.

Anyone have any ideas on anything I am doing that is obviously wrong,
but which I am missing?

thanks,

-vijai.
 
M

Mike Wahler

Re: Using std::strinstream to convert a char to int

That doesn't make any sense. Type 'char' objects
already are integers. stringstreams are for working
with strings. Neither a 'char' nor an 'int' are strings.
More below.
I wrote the following function as a curiosity:

template<typename SourceType,
typename DestinationType>
DestinationType NumericCast(const SourceType& value) {
std::wstringstream strbuf;
strbuf << value << std::endl ;
strbuf.flush();
DestinationType convalue;
strbuf >> convalue;
return convalue;
}

This works fine for all numeric types such as int to long, long to
float etc.

However, I tried the following:

NumericCast<char, int>('c');

This failed to return the correct value.

That's because you probably don't understand what
'correct value' means with a stringstream as you're
using it.
When I stepped through the
function, the value of "convalue" remained the same throughout. But
this happened only when SourceType was char. A

I tried stepping into the operator code, but my debugger just stepped
over so I am not sure what's going on inside.

Anyone have any ideas on anything I am doing that is obviously wrong,
but which I am missing?

If you want to convert a type 'char' to a type 'int', simply write:

char c = 42;
int i = 0;

i = c;

What you're probably not realizing is that what a stringstream
does with numeric objects is translate the *text* representation
of a value (e.g "123") to its binary representation ( int(123) ),
or the inverse conversion ( int(123) to "123"). Sinc both 'char'
and 'int' are already numeric types, there's no text involved.

There's a reason that type 'stringstream' has the word 'string'
as part of its name. :)

-Mike
 
V

Vijai Kalyan

Hi Mike. Thanks for your reply. That is interesting to know.

I read online a long time about how we can convert numeric values to
their string representations by using a stringstream. Or was that a
stringbuf? What's the difference?

Anyway, I was playing around with the above simple as an extension of
that idea.

But, I am still cuious about why it still works for the other numeric
types but not for a char.

regards,

-vijai.
 
M

Mike Wahler

Vijai Kalyan said:
Hi Mike. Thanks for your reply. That is interesting to know.

I read online a long time about how we can convert numeric values to
their string representations by using a stringstream.

That would be a 'ostringstream'. 'istringstream' is
for the inverse, and 'stringstream' can do both.
Or was that a
stringbuf? What's the difference?

'stringbuf' is a supporting type used by the 'stringstream'
types in their implementation.
Anyway, I was playing around with the above simple as an extension of
that idea.

But, I am still cuious about why it still works for the other numeric
types but not for a char.

It does work. You just need to remember that type
'char' is an integer type, and that it's not a string.

#include <iostream>
#include <sstream>
#include <string>

int main()
{
char c = 'X';
std::eek:stringstream oss;
oss << c;
std::string s(oss.str());
std::cout << s << '\n'; /* prints X */
std::istringstream iss(std::string(1, 'A'));
iss >> c;
std::cout << c << '\n'; /* prints A */
return 0;
}

-Mike
 
I

Ian

Vijai said:
Hi Mike. Thanks for your reply. That is interesting to know.

I read online a long time about how we can convert numeric values to
their string representations by using a stringstream. Or was that a
stringbuf? What's the difference?

Anyway, I was playing around with the above simple as an extension of
that idea.

But, I am still cuious about why it still works for the other numeric
types but not for a char.
Because the string representation of any other numeric type is a number,
while the string representation of 'c' is 'c'.

streaming 'c' into and int isn't a good idea.

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top