num to char string

Started by
12 comments, last by GamerCon 19 years, 4 months ago
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
OS: WinXP HECOMPILER: DevC++ mingw32Programming since 10/3/2004 6:15:36 PMCurrent Project: An un-named MUDCompleted Projects:Tic-Tac-Toe (w32 console)PacMan (w32 console)Tetris (w32 console)Minesweeper (Allegro)BomberMan (Allegro)Chat Client (w32 console)
Advertisement
snprintf() works too.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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);
Whoops, make that sprintf, not fprintf, sorry!
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().
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
OS: WinXP HECOMPILER: DevC++ mingw32Programming since 10/3/2004 6:15:36 PMCurrent Project: An un-named MUDCompleted Projects:Tic-Tac-Toe (w32 console)PacMan (w32 console)Tetris (w32 console)Minesweeper (Allegro)BomberMan (Allegro)Chat Client (w32 console)
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 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.
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.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
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...

This topic is closed to new replies.

Advertisement