# [C++]Int to Char

## Recommended Posts

I have an integer value, but I want it to be shown as a char. I don't mean ASCII values, I mean that if the int is equal to say, 128, I want the values 128 shown as chars. String class is applicable here too, probably would be easier for me? Thanks.

##### Share on other sites
Look into the itoa() function. It is in the stdlib.h section.

Much luck!

##### Share on other sites
with c++

do this

#include <sstream>
#include <string>

using namespace std;

string to_string( int val )
{
stringstream stream;
stream << val;
return stream.str();
}

// or more generally

template< class Type >
string to_string( Type val )
{
stringstream stream;
stream << val;
return stream.str();
}

i learned this yesterday, so i may be *very* wrong

##### Share on other sites
rip-off is correct. Use stringstreams. In my opinion, they are the easiest way to do this. They're a great thing to have in your toolbox.

##### Share on other sites
Wasn't there a thread recently in which someone explained why not to use stringstreams? (bad performance, memory leaks etc.) Anyway, I've seen a benchmark somewhere (not sure where) in which itoa() was about 6 times as fast as std::stringstream.

##### Share on other sites
Quote:
 Original post by KalasjniekofWasn't there a thread recently in which someone explained why not to use stringstreams? (bad performance, memory leaks etc.) Anyway, I've seen a benchmark somewhere (not sure where) in which itoa() was about 6 times as fast as std::stringstream.

There's something seriously, seriously wrong with your program if converting from a string to an int is a bottleneck. Speed is irrelevant. Memory leaks would be surprising, unless you forget to delete the characters you pass to it.

##### Share on other sites
Sorry for such a dumb question, I completely forgot about all the conversion functions, getting back into the C++ groove. ;)

Thanks guys

##### Share on other sites
How about the old trusty sprintf? It's what I use most of the time.

##### Share on other sites
Quote:
 Original post by parklifeHow about the old trusty sprintf? It's what I use most of the time.

you misspelled snprintf there

##### Share on other sites
Quote:
Original post by rip-off
Quote:
 Original post by parklifeHow about the old trusty sprintf? It's what I use most of the time.

you misspelled snprintf there

There's pretty much no reason to use snprintf in this situation.

##### Share on other sites
Quote:
 Original post by rip-offyou misspelled snprintf there

So sprintf is a no-no?

##### Share on other sites
i read that sprintf and so on are major causes of bugs, so better to be safe than sorry.

snprintf is safer in my opionion, with the stringstream being safer again

what if you get on fine with sprintf and continue to use it in other situations until there is one that can cause a bug.

i don't like to encourage its use, esp in the beginners forum

##### Share on other sites
i suggest the stringstream method or boost::lexical_cast.
boost::lexical_cast<std::string>(myInteger)

##### Share on other sites
Quote:
 Original post by rip-offi read that sprintf and so on are major causes of bugs, so better to be safe than sorry.snprintf is safer in my opionion, with the stringstream being safer againwhat if you get on fine with sprintf and continue to use it in other situations until there is one that can cause a bug.i don't like to encourage its use, esp in the beginners forum

snprintf is not necessarily safer than sprintf. It just produces different kinds of security holes.

##### Share on other sites
If you don't need to *store* that text data (the chars '1', '2', '8' in sequence) but just output them, then - just output them. Streams are already templated to handle things properly according to the type of what you output. (In some circumstances you can get 'other' behaviour via explicit casts, because templates make use of the static, exact type of parameters.)

int a = 65;cout << "The ascii value for '" << static_cast<char>(a) << "' is " << a << endl;

If you want to store the value somewhere, use a std::stringstream. It's a kind of stream, so you work with it just like the console (which is why you can convert things "magically" - it's using the same code that the console streams do), except its "source/destination buffer" is a string in memory. Since it's not explicitly an input or output stream, you can do both; so the stringstream technique works by putting the value into the buffer as an int (doing the conversion), then pulling it back out as a std::string (doing another conversion back that way).

The boost::lexical_cast basically is a wrapper for the stringstream method, by the way: it does a bit of extra work to make things safer, more or less.

##### Share on other sites
most of the times i use itoa... and sometimes i use:
int num = 9;CString cs;cs.Format("%d", num);

I never know what kind of strings are the best to work with... CString? std::string? or plain char*?

thanks

##### Share on other sites
Quote:
 Original post by fuchiefckI never know what kind of strings are the best to work with... CString? std::string? or plain char*?

If you are doing MFC programming, CString is the best suited. Other than that you have to include MFC header files to get access to it.

For general programming, std::string will often suffice and for the most part, using the std::string will make your life easier. Highly reccomended for beginners who are not yet accustomed to pointers.

For programmers that are not beginners, char* or std::string are used on an as needed basics.

It all just depends on the situation and the programmer. There was a big thread on this in the For Beginner's forum, so if you want more information on this topic, you can check the archives. I don't have it bookmarked, but maybe someone else does.

##### Share on other sites
[edit: OT... I concur with Gumpy MacDrunken on the original point.]

I recently switched to using std::string primarily, after a long stubborn grudge with char *'s. Using std::string isn't much easier to use, but the main advantage is simply less code to write since the std::string will be automatically cleaned up on function exit or class deletion.

Less code to write means faster development, and less chance for human mistakes.

##### Share on other sites
Quote:
 Original post by chbrulesI have an integer value, but I want it to be shown as a char. I don't mean ASCII values, I mean that if the int is equal to say, 128, I want the values 128 shown as chars. String class is applicable here too, probably would be easier for me? Thanks.

I figure you got multiple answers by now, but just for study, why not just use a union?

// Shares the same memory location. union u_type{    int i;    char ch;};int main(){       u_type cnvt;    cnvt.i = 65;       cout << cnvt.ch << endl;  // outputs 'A'    return 0;}

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978702

• 21
• 14
• 12
• 10
• 12