Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert integer to string

Posted on 1999-07-05
6
Medium Priority
?
447 Views
Last Modified: 2010-04-02
I want to convert any integer into a similar string:
e.g, with the integer 97, i want to obtain the CString "97"
with the integer 255, I want to obtain "255"
Any algorithm already coded out there ?
Thanks
0
Comment
Question by:tradinfo
[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
6 Comments
 
LVL 2

Accepted Solution

by:
JensUniweb earned 120 total points
ID: 1199139
In C you normaly use itoa(). But that is for a char*. CString is a MicroSoft thing. Not standard C++. In standard C++ you might do it using a string stream.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1199140
For a CString, you are probably best off using the Format member, like this:

#include <afx.h>
#include <stdlib.h>

#include <iostream>

using namespace std;

void main()
{
    int i = 97;
      
    CString cs;
    cs.Format("%d",i);

    cout << (const char*) cs << endl;
}
0
 

Expert Comment

by:pride
ID: 1199141
i usually use sprintf... Here is example code :

#include <stdio.h>

int main(void)
{
   int number = 97;
   char buffer[80];

   sprintf(buffer, "%d", number);
   puts(buffer);
   return 0;
}
0
Independent Software Vendors: 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!

 
LVL 5

Expert Comment

by:yonat
ID: 1199142
If you need to do this in many places in your code, I find it useful to have a function that takes an int and returns a string, using any of the above mentioned techniques. You can use my own "stringizer", available at http://www.kinetica.com/home/yon/4dev/ .
0
 

Expert Comment

by:kappert
ID: 3546475
// Demonstrates conversion NUMBER -> STRING in C++
// Taken from comp.lang.c++

#include <iostream>
#include <sstream>

using namespace std;

int main(void)
{
  string buf;
  ostringstream out(buf);
  int someNumber = 1234;
  // place formatted items in buffer
  out << "Number converted to string: " << someNumber << ends;
  // use out.str() for accessing the string
  cout << out.str() << endl;
  return 0;
}
0
 

Expert Comment

by:kappert
ID: 3547195
// the answer previously provided contained unnecessary
// complexity. For our project, I'll use the following style.

#include <iostream>
#include <sstream>

using namespace std;

int main(void)
{
  ostringstream myO;
  myO << 77;
  cout << myO.str() << endl;
 
  return 0;
}      
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

661 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