Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert an Integer to a String

Status
Not open for further replies.

thoms314159

Technical User
Apr 8, 2003
13
0
0
CA
I am looking for a way to convert an integer to a string (please note that I mean a variable of type "string", not a C style string). I can currently do it using a couple of ugly lines of codes, but I was wondering if anyone new of a function that would get the job done. If anyone knows of a function that converts a C style string to an actual string, that would be of use too.

Thanks,
Jon
 
I would use ATL:

#include<atlbase.h>
#include<comip.h>
#include<comdef.h>
#include<comutil.h>

int main()
{
_bstr_t x = &quot;number:&quot;;
x += _bstr_t(10);
cout<< (char*)x<< endl;
x += &quot;hello&quot;;
cout<< (char*)x<< endl;
return 0;
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
string s;
int x = ...;
...
{
char junk[16];
s.assign(itoa(x,junk,10));
}
 
>If anyone knows of a function that converts a C style string to an actual string

Simply assign the c-style string to the std::string.

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
After all some syntax sugar:
string& operator << (string& s, int x)
{ // can't redefine string class assign op
char junk[16];
sprintf(junk,&quot;%d&quot;,x); // sprintf - ISO Std, itoa - not
s.append(junk);
return s;
}
...
s << 2003;
 
Complete answer:

Read the FAQ at the C++: UNIX forum.


Short answer:

The most Standard way to do it is with stringstreams. Other ways mentioned, such as itoa and ATL, work, but are non-Standard.

Getting it to a char* through something like sprintf and then converting that to a string is Sandard, of course.

has a lexical_cast &quot;operator&quot; that does this, and will probably be Standard in the near future.
 
>ArkM
is your code more simple than the ATL one:
_bstr_t x = &quot;number:&quot;;
x += _bstr_t(10);
cout<< (char*)x<< endl;//?

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
IonFilipski,
no, my code is not a simplest one.
It's 100% platform-independent (STL only) and 100% Std compl.
I think, it's trivialities, needles to say.
Best Regards!
 
>ArkM
>It's 100% platform-independent
it is 100% while there are only your five lines of code, for teaching purposes. But when you write a real application, it will be at most 5% platform independend [LOL]. So when you use Microsoft C++ you should take all advantages of it. By the way, ATL is also available in BorlandC++ builder and simple BorlandC++.
So in real projects where you need just to implement, you will always forget about platform and compiller independency.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
IonFilipski,
recently I took part in a real C++ project (XML, JNI, sockets, threads etc, near 100000 source lines) with more than 5% platform-independency (Windows, Solaris, Linux). Platform-dependent code was about 5% or less. That's not a question.
But let's remember our topic. Yes, of course, your sample code above is more better than mine.
Happy weekend!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top