Jump to content
  • Advertisement
Sign in to follow this  
GamerCon

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Whoops, make that sprintf, not fprintf, sorry!

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by alnite
You 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 this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by alnite
You 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 this post


Link to post
Share on other sites
Quote:
Original post by GamerCon
The 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 this post


Link to post
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!