By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,437 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,437 IT Pros & Developers. It's quick & easy.

Using std::stringstream to convert a char to int

P: n/a
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.

Nov 6 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Vijai Kalyan" <vi**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

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
Nov 6 '05 #2

P: n/a
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.

Nov 6 '05 #3

P: n/a

"Vijai Kalyan" <vi**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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::ostringstream 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
Nov 6 '05 #4

P: n/a
Ian
Vijai Kalyan wrote:
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
Nov 6 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.