[C++]Int to Char

Started by
17 comments, last by dxFoo 18 years, 7 months ago
Quote:Original post by rip-off
you misspelled snprintf there


So sprintf is a no-no?

Advertisement
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
i suggest the stringstream method or boost::lexical_cast.
boost::lexical_cast<std::string>(myInteger)
This space for rent.
Quote:Original post by rip-off
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

snprintf is not necessarily safer than sprintf. It just produces different kinds of security holes.
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.
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
-fuchiefck----------------------------------------------------------"Inside the world that you as a programmer or developer create, you are God" - Zerbst & Duvel
Quote:Original post by fuchiefck
I 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.
[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.
Quote:Original post by chbrules
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.


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;}

This topic is closed to new replies.

Advertisement