• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

int to CString

like the title says... i need to convert a int to a cstring and NOT a char
0
zeurx
Asked:
zeurx
1 Solution
 
RalphWiggumCommented:
There's probably a better way to do this, but it works:

CString string;
TCHAR hold[4];
_stprintf(hold, _T("%d"), 35);
string = hold;
0
 
jadams117Commented:
The CString class has a function, Format() which behaves just like sprintf()..

CString str;
str.Format("%d", 100);

Jad
0
 
nickolay_kolevCommented:
Easy:

#include<sstream>
using namespace std;

string itos(int i)
{
     stringstream s;
     s << i;
     return s.str();
}
0
 
reidy_boyCommented:
If you were to declare your string as char*, all the usual functionality that comes with CString will work just as well.
0
 
reidy_boyCommented:
Alternatively, you could use this function

CString convert_int_to_cstring(unsigned int value)
{
CString return_value = "";
unsigned int tens, units;

     if (value > 99)
          return (return_value);

     tens = units = 0;

     while (value%10) {
          value--;
          units++;
     }

     while (value%100) {
          value-=10;
          tens++;
     }

     return_value += char(tens);
     return_value += char(units);

     return (return_value);
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now