Public Group

num to char string

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

Recommended Posts

Im trying to write a M.U.D. and need to send data pertaining to health,mana,movement points, ect. The only way i know how to convert an int to a char is to use a fairly annoying process. ie. int num = 876354; ostringstream oss; oss << num; char *str = (char *)oss.str().c_str(); is this the best/only process? Thanks for any help Thanks to all who have helped me in the past -GamerCon

Share on other sites
snprintf() works too.

Share on other sites
Well, if you want to use C you can do something like:

int some_number = 420;
char temp_string[80];
fprintf(temp_string, "%d", some_number);

Share on other sites
Whoops, make that sprintf, not fprintf, sorry!

Share on other sites
You can use itoa() or sprintf()

char buffer[BIG_ENOUGH];

itoa( num, buffer, 10 );
sprintf( buffer, "%i", num );

snprintf() is a Linux function.

edit:
VS.NET has _snprintf().

Share on other sites
sweet, i cant believe i didnt know that...Guess thats a drawback of not learning C before C++.

oh, and i thought itoa() converted the int into its character value
sutch as 50 = '1' the equivilant of (char)50?

Thanks again.
-GamerCon

Share on other sites
Quote:
 Original post by alniteYou can use itoa() or sprintf()

itoa() is severely non-standard; there are at least six different commonly used itoa() function signatures.

Quote:
 snprintf() is a Linux function.edit:VS.NET has _snprintf().

It's a common C89 vendor extension, and was standardized in C99. If you have the option, you should almost always choose to use snprintf() over sprintf() since sprintf() is prone to buffer overflows.

Share on other sites
Quote:
Original post by SiCrane
Quote:
 Original post by alniteYou can use itoa() or sprintf()

itoa() is severely non-standard;

Yes, but it does the job.

@GamerCon:
No. itoa() doesn't convert a character to its ASCII value, it already is. There is another function called atoi() which does the opposite, turns a string into a number. A string "1249" is converted to 1249.

Quote:

Quote:
 snprintf() is a Linux function.edit:VS.NET has _snprintf().

If you have the option, you should almost always choose to use snprintf() over sprintf() since sprintf() is prone to buffer overflows.

Yes, I am aware of that. Last time I tried using it in VC, snprintf() is not defined elsewhere. VC has _snprintf() instead, which I assume a VC specific function (hence the underscore). A search on Google come up with some third party implementations of snprintf(). So, I assumed it's non-standard.

I was just giving all the options the OP has, buffer-overflow prone or not, standard or not, as he asked it.

Share on other sites
Quote:
 Original post by GamerConThe only way i know how to convert an int to a char is to use a fairly annoying process.int num = 876354;ostringstream oss;oss << num;char *str = (char *)oss.str().c_str();
what you're doing here is really not a good idea, we can't determine when oss will change and also you then assign a non-const pointer (thus 'allowing' write access to the address returned by c_str() -- not good). IMHO its good practice to always using std::string's, i almost never use char* now adays.

Quote:
 is this the best/only process?
create an inline function that returns an std::string, the compiler will optimize away the return value and function call in release mode (as long as you keep the function short or use the __forceinline keyword in MSVC)
inline std::string int2str(int i) {  ostringstream oss;  oss << num;  return oss.str();}
this would probably be the best non-c style way to do what you want.

Share on other sites
When you "send the data", are you talking client->server or server->client?

Assuming the latter (since "MUD" suggests allowing for raw telnet/ordinary MUD clients), don't bother with this conversion, because you'll be dumping the output into a stream anyway. If you like, you could take whatever object you have that represent a client connection and make it behave like an iostream object (if it doesn't already), then just do things like:

Connection c;// Stats are stored as ints; shifting them onto a stream// "just works"c << "<" << hp << " hp " << mp << " mp " << mv << " mv>";

If the former, why not send the data in binary instead? You'll only have to convert it back on the server end otherwise...

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 10
• 11
• 37
• 12
• 12
• Forum Statistics

• Total Topics
631414
• Total Posts
2999958
×