num to char string
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
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);
int some_number = 420;
char temp_string[80];
fprintf(temp_string, "%d", some_number);
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().
char buffer[BIG_ENOUGH];
itoa( num, buffer, 10 );
sprintf( buffer, "%i", num );
snprintf() is a Linux function.
edit:
VS.NET has _snprintf().
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
oh, and i thought itoa() converted the int into its character value
sutch as 50 = '1' the equivilant of (char)50?
Thanks again.
-GamerCon
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.
Quote:Original post by SiCraneQuote: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.
Quote:Original post by GamerConwhat 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.
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();
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.
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:
If the former, why not send the data in binary instead? You'll only have to convert it back on the server end otherwise...
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...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement