Sunday, August 2, 2009

In C++, how do you convert an integer into a string or character?

For example, I want to be able to read in the int 65 and for it to return the string or char "a". I want it to do this for every number entered. Is there a member function that does this?

In C++, how do you convert an integer into a string or character?
Char to Int





char word = 'a';


cout %26lt;%26lt; (int)a;


Displays: 65





Int to Char





int Number = 65;


cout %26lt;%26lt; (char)Number;


DIsplays: a
Reply:try


itoa();





function ,its a C function though will work fine,


im bit sure...
Reply:Msdn is a great source for code and examples. However like in most other languages you would use the TryParse method. For example in C# it would be:





int one;


char[30] two;





Tryparse.Int32(one, two);
Reply:the first answer: casting the int to char using (char) a is the correct answer.





itoa() will return the string "65".





There is nothing in C++ like TryParse. Dear answerer, if you have learnt something in some language or it's associated library, try and keep it limited to that language and not assume that it is a very common thing available in all programming languages.





Actually the commonality between programming languages are more of exceptions than rules.
Reply:try (char)


like


int a = 65;


char b = (char)a ;


No comments:

Post a Comment