How to use itoa() in C++?

I

Immortal Nephi

I want to use itoa() function to convert from integer to string. I
looked at documenation. itoa() function is C sytle header like
string.h and stdlib.h. I am unable to find iostream library which itoa
() function should be built in string class like #include <string>,
but NOT #include <string.h> or #include <cstring>.

For example.

unsigned long i = 0x21ae;
string s1("integer is converted to string: ");

string s2 = itoa( i ); // ????

string s3 = s1 + s2;

cout << s3 << endl;


Output -- "integer is converted to string: 0x21ae".
 
V

Victor Bazarov

Immortal said:
I want to use itoa() function to convert from integer to string. I
looked at documenation. itoa() function is C sytle header like
string.h and stdlib.h. I am unable to find iostream library which itoa
() function should be built in string class like #include <string>,
but NOT #include <string.h> or #include <cstring>.

There is no 'itoa' function in standard C or C++.
For example.

unsigned long i = 0x21ae;
string s1("integer is converted to string: ");

string s2 = itoa( i ); // ????

string s3 = s1 + s2;

cout << s3 << endl;


Output -- "integer is converted to string: 0x21ae".

Use 'std::eek:stringstream'.

V
 
I

Immortal Nephi

There is no 'itoa' function in standard C or C++.







Use 'std::eek:stringstream'.

V,

I can't find 'std::eek:stringstream' example code in the iostream
library. Can you be kind to give me your example code, please? Thank
you.
 
J

Jorgen Grahn

There is no 'itoa' function in standard C or C++.

IIRC, it was a common C extension in the distant past.

But I am pretty sure it never rendered the integer in hexadecimal.
Use 'std::eek:stringstream'.

Or sprintf(), wrapped in a function which he can call itoa() if he
wants. No risk of buffer overflow in this case.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top