Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 6-Apr-12 1:05am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
Member 8446342 at 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 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

how are you "displaying" str ?
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 6 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100