?
Solved

int to CString

Posted on 2003-03-19
5
Medium Priority
?
442 Views
Last Modified: 2013-11-20
like the title says... i need to convert a int to a cstring and NOT a char
0
Comment
Question by:zeurx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:RalphWiggum
ID: 8170013
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
 
LVL 1

Accepted Solution

by:
jadams117 earned 40 total points
ID: 8170645
The CString class has a function, Format() which behaves just like sprintf()..

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

Jad
0
 

Expert Comment

by:nickolay_kolev
ID: 8171097
Easy:

#include<sstream>
using namespace std;

string itos(int i)
{
     stringstream s;
     s << i;
     return s.str();
}
0
 
LVL 1

Expert Comment

by:reidy_boy
ID: 8174390
If you were to declare your string as char*, all the usual functionality that comes with CString will work just as well.
0
 
LVL 1

Expert Comment

by:reidy_boy
ID: 8174425
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question