Click here to Skip to main content
12,951,971 members (64,232 online)
Rate this:
 
Please Sign up or sign in to vote.
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 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 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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 6 Apr 2012
Copyright © CodeProject, 1999-2017
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