Public Group

# _itoa_s use

This topic is 4275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Alright, I need to convert an integer to and std::string and the only method I know is to use _itoa_s to create a char buffer, then create th' string based on that. So, for some reason I am not doing it correctly. I get some kind of overflow error. Here is the error text: Debug Assersion failed Program: ... File: f:\rtm\vctools\crt_bld\self_x86\crt\src\xtoa.c Line: 142 Expression: length < sizeInTChars For information blah blah blah blah. Alright, so that is the message I am getting. It's probably to do with the stupid _itoa_s and my not know what I am doing :) So, here is the code that is triggering it:
char tempbuf[3] = { ' ',' ',' '};
_itoa_s( m_weapon_mod, tempbuf, 10);
std::string weapon_mod_s(tempbuf);


Later on, I print this, but as far as I know that is error free (I cannot progress farther into the program w/o fixing this... I am I doing this correctly? I only need 3 characters. like "123". That's it. So... am I doing this the best way? am I even using _itoa_s correctly? Thanks...

##### Share on other sites
Old C style functions like _itoa_s and C style strings don't belong anywhere near C++. Use a stringstream to convert between normal types.

std::stringstream stream;
stream << 10;
std::string weapon_mod_s = stream.str();

##### Share on other sites
Look up string streams:
#include <sstream>#include <string>int main(){   std::stringstream str;   str << 5;   std::string s = str.str();   cout << s << endl;}

CM

##### Share on other sites
You are using it correctly. The error you are getting is a result of tempbuf not being large enough to hold the result of the number in m_weapon_mod. You need room for the number and the '\0' that it puts at the end of the string.

So, to convert:

int x = 123;

you need:

char tempbuf[4];

since _itoa_s will want to write "123\0" to the output buffer.

##### Share on other sites
The third parameter to _itoa_s is the size of the buffer in characters, so if your "tempbuf" contains 3 characters, you should pass in 3 rather than 10. Also note that tempbuf needs room for the trailing NULL character, so you should really have a 4-character buffer and pass in 4 as parameter 3, when trying to convert a 3-digit number.

However, if you are programming C++ rather than C, I second (third?) the recommendation to use a stringstream, or boost::lexical_cast.

##### Share on other sites
Thanks, I googled this, but all I got was th' silly _itoa_s. :/ Why didn't it get something about stringstreams?

Thanks everyone

##### Share on other sites
Quote:
 Original post by bakery2k1The third parameter to _itoa_s is the size of the buffer in characters, so if your "tempbuf" contains 3 characters, you should pass in 3 rather than 10. Also note that tempbuf needs room for the trailing NULL character, so you should really have a 4-character buffer and pass in 4 as parameter 3, when trying to convert a 3-digit number.However, if you are programming C++ rather than C, I second (third?) the recommendation to use a stringstream, or boost::lexical_cast.

Actually there are 2 versions of _itoa_s .. one is templated and the size is acquired from the buffer. He happens to be using that one. He is calling it correctly, his buffer just isn't large enough.

*Edit*

Here are the declarations of the two versions:

errno_t _itoa_s(   int value,   char *buffer,   size_t sizeInCharacters,   int radix );template <size_t size>errno_t _itow_s(   int value,   wchar_t (&buffer)[size],   int radix ); // C++ only

He happens to be using the 2nd one.

##### Share on other sites
Thanks for correcting me on that. So 10 is being passed as the number base, rather than the buffer size. OK.

##### Share on other sites
Quote:
 Original post by PlasmaroboThanks, I googled this... why didn't it get something about stringstreams?Thanks everyone

Good heavens, you're right. Fellow experts - this means war. (Although I don't get anything about _itoa_s either.)

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999487
×