Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After converting a string variable into int using _ttoi function , I need to convert it to String to display the value. This is the code :
My function:  

int i;
char buffer[20];
CString str;

i= _ttoi(myVal->GetText());  //convert string to int 
itoa (i,buffer, 10); //convert int back to string 

str.AppendFormat(_T("Value is : %s\n"), buffer);


But this does not display anything at all!!! Any ideas?
Posted

You don't need to use atoi at all. CString::AppendFormat or CString::Format does exactly what you want to do:

int i = 4711;
CString str;
str.Format (_T("My variable has value: %d\n"), i);


CString's formatting capacity is one of the best and very flexible.
 
Share this answer
 
Comments
Sumal.V 6-Apr-12 9:32am    
Well thanks,
Had done major blunder by adding _ttoi instead of _ttof and yeah finally managed to get things in place.
cheers :)
If this a trick question? You're mixing chars and wide chars. On one hand you use _ttoi and _T() and then char and itoa. And you probably build with UNICODE char set.

Make it like this and it will work.
int i;
TCHAR buffer[20];
CString str;

i= _ttoi(myVal->GetText());  //convert string to int
_itot(i, buffer, 10); //convert int back to string

str.AppendFormat(_T("Value is : %s\n"), buffer);


But why do you use itoa at all? Simply use CString::AppendFormat() or CString::Format() as already pointed out.
 
Share this answer
 
how are you "displaying" str ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900