473,388 Members | 953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Using std::stringstream to convert a char to int

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
4 12119
"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Giampiero Gabbiani | last post by:
Is it possible to reset a std::stringstream in order to reuse it once more? I tried with flush() method without any success... Thanks in advance Giampiero
5
by: Mr Fish | last post by:
Is it possible for me to record floats in a std::stringstream with 100% accuracy? If so how? Here's a little prog that demonstrates how the default ss loses accuracy ...
5
by: Marcin Kalicinski | last post by:
Is there a vectorstream class that implements the functionality similar to std::stringstream but with std::vector, not std::string? cheers, Marcin
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
2
by: avidamani | last post by:
Hi, I have a c++ application compiled using Compaq C++ V6.5-014 for Compaq Tru64 UNIX V5.1A (Rev. 1885) Compiler Driver V6.5-014 (cxx) cxx Driver The problem is that the application is...
1
by: magix | last post by:
I got this reply in my previous post a month ago: May I know, how can I automatically create the folder if it doesn't exist ? In previous reply, it said: -------------------------...
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
7
by: Grey Alien | last post by:
Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string?. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
7
by: TBass | last post by:
Hi. I wrote a "tag" class to store values. The user gets to store most any type he would need. Instead of getting too complicatated, I decided I would store the value as a stringstream, then...
3
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.