Sign in to follow this  
GamerCon

num to char string

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
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
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
I dont realy care about C or non-C styles, I just want to get the job done as quickly and efficiently as possible.(less bandwidth)

I'm using winsock and created a server that interacts with a telnet client IMO quite nicely. I'm also using a mingw32 compiler(DevC++);

the problem with using the string header file with sstream is that winsock doesnt allow to send std::strings only const char's so a conversion from string to char is needed before sending.

If it is possible to send binary using winsock, I dont know how.

as for sending and reciveing im mainly talking about server to client but im adding code that allows designated players(map builders) to log on and create rooms, so certain entries will require the sending of integers.(ie. "/makedoor direction x,y,z" or "/give paul 1000 silver")

Connection c;

// Stats are stored as ints; shifting them onto a stream

// "just works"

c << "<" << hp << " hp " << mp << " mp " << mv << " mv>";

I have no real grasp on what the above is doing, i've found very little documentation on programming with winsock for a plain win32 console which leaves me to reading the header files and trying stuff out for myself although beej's guide is very helpfull and Richard Halletts non-blocking tutorial was great but one part eludes me.

His check_messages() function has a loop that adds all the client sockes connected to an input_set and exc_set then ads 1 to nfds every pass which would equal 1 less than the number of connections when the loop is completed. In beej's guide it states that the numfds arg in select() must be 1 more than the highest socket value and my server socket is usualy something in the range of 324 and increases around 28-32 for each additional connected socket.

Also in beej's guide where he has a sample on using select(), there's a part that loops from 0-highest socket value incrementing by 1 which seems very wrong to me unless linux's sockets start at 0 and increment by 1 for every connected socket.

anyway, thanks for all the help, I'm going to play around with my server using sprintf and read up on snprintf, its defined somewhere but the last arg which im inputing as an int keeps giving me an error that it cannot convert a const char to an unsigned int.

example of snprintf()

snprintf(buffer,"%s%sYou moved north. %i,%i,%i\n\r",
client.player.fgColor,
client.player.bgColor,
client.player.x,
client.player.y,
client.player.z); // cannot convert const char to unsigned int

-GamerCon

Share this post


Link to post
Share on other sites

int num = 1337;
char buffer[255];

// Load the number into the character buffer (decimal)
itoa(num, buffer, 10);

// Display the number character by character
for(int i = 0; i<4; i++)
std::cout << buffer[i];


Share this post


Link to post
Share on other sites
I see where i was going wrong with itoa(), i looked up the third arg and its a radix value, so using 2 as the radix will convert the number into binary string and 10 will convert it into decimal string. Very cool. Although, I cant fgure out how to convert bin/oct/hex back into dec.

no matter yet, ill be using snprintf for my mud since it requires me to send actual words and numbers and VT100 code all in one packet. Aswell atoi will be used for converting certain parts of client packets into ints.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this