views:

495

answers:

9

how to convert integer into string pointer in visual c++?

A: 

If you want a textual representation of the pointer address use sprintf. If you want to treat the numeric value as a pointer to a string use casting like so:

int intValue = ...;
char * charPtr = (char*)intValue;
yngvedh
He said convert, so I'm pretty sure he means turning 123 into "123", not a cast.
GMan
this command is worable if one wants to convert in char pointer but this is not working for converion to string pointer.
dome
@GMan yes exactly , iwant to convert 123 in "123".how to do that?
dome
I would say do what pierr is suggesting in his answer.
GMan
A: 

You got homework? a generic one, tested if g++, http://effocore.googlecode.com/svn/trunk/devel/effo/codebase/addons/inl/include/impl/algo_impl.h :

#ifdef __cplusplus

static inline char *int2a_put(uintptr_t i, char *s)
{
    do {
     *s++ = '0' + i % 10;
     i /= 10;
    } while (i);

    return s;
}

static inline void int2a_reverse(char *head, char *tail)
{
    for (*tail = '\0'; --tail > head; ++head) {
     /* exchange */
     (*head) ^= (*tail);
     (*tail) ^= (*head);
     (*head) ^= (*tail);
    }
}

template<typename t>
static inline const char *int2a(t i, char *s)
{
    char *p;
    char *ret = s;
    bool f = false;

    p = s;
    if (i < 0) { 
     *p++ = '-';
     ++ s;
     /* 
      * In limits.h, INT_MAX was defined as 
      *   maximum values a `signed int' can hold.
      * and LONG_MAX was defined as maximum values 
      *   a `signed long int' can hold. 
      */
     switch (sizeof(t)) {
     case 8:
      {
       /* 
        * Inject \p a to prevent from complaint 
        *   of compiler.
        */
       ef64_t a = (ef64_t)i;
       if (-LLONG_MAX > a) {
        i = (t)LLONG_MAX;
        f = true;
       }
      }
      break;
     case 4:
     case 2:
     case 1:
      {
       /* 
        * Inject \p a to prevent from complaint 
        *   of compiler. 
        */
       int a = (int)i;
       if (-INT_MAX > a) {
        i = (t)INT_MAX;
        f = true;
       }
      }
      break;
     default:
      break;
     }
     if (!f) {
      i = -i;
     }
    }

    p = int2a_put((uintptr_t)i, p);

    if (f) {
     ++ *s;
    }

    int2a_reverse(s, p);

    return ret;
}

/*
 * No "static" otherwise g++ complains 
 *   "explicit template specialization cannot have a storage class"
 */
template<>
/*static*/ inline 
const char *int2a<uintptr_t>(uintptr_t i, char *s)
{
    char *p = int2a_put(i, s);

    int2a_reverse(s, p);

    return s;
}

#else
EffoStaff Effo
+1  A: 

If you using CString, then you can use Format() method like this:

int val = 489;
CString s;
s.Format("%d", val);
Naveen
+2  A: 

search for atoi / itoa in your favorite documentation. Or try Boost (www.boost.org - library Conversion, lexical_cast).

Both ways are portable across different compilers.

Tobias Langner
I prefer lexical_cast myself. It looks cleaner than std::stringstream and itoa. PLus itoa is listed as "non-standard" on cplusplus.com. I know boost isn't technically standard either, but I figure it's better than relying on your compiler to have a non-standard cstdlib function.
KitsuneYMG
+1  A: 

Take any C and C++ textbook. This simple C code should work in Visual C++ and others C++ compilels and convert 489 into "489":

char result[100];
int num = 489;
sprintf(result, "%d", num);
Michał Niklas
+4  A: 

Use stringstream

#include <sstream>
stringstream ss;
ss << i;
string   s = ss.str();
pierr
visual c++ is not taking #include <sstream>header file so this methid is not workable.
dome
visual c++ should take sstream.. there must be something wrong with your setup. This is definately the preferred/safer way to do it. char*, atoi, sprintf, etc all open the door for the programmer to make errors. It is best to be safe and stick to using std::string instead of char*, and stringstream for conversions. This comes at the expensive of performance... but barely. For most applications this will do fine
Fuzz
come on guy, why not write your own itoa but use low perfromance string or CString? btw, sprintf is slow too. you got bad idea
EffoStaff Effo
A: 

There is a very easy method

int i=4;
String ^ s = Convert::ToString(i);
avd
A: 

basic C++

char text[100];
int num=123;
itoa(num,text,10);
Andrew Keith
A: 

This is how I did it in my homework since we were only allowed to use some predetermined libraries. I'm pretty sure it's not considered a "best practice" though ;)

string int2string(int integer) {
    string str;
    int division = integer;

    while (division > 0) {
     str = char('0' + (division % 10)) + str;
     division = division / 10;
    }

    return str;
}
Olivier Lalonde