Jump to content
  • Advertisement
Sign in to follow this  

Converting an int to a const char*

This topic is 4968 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

Hi, I am currenty trying to convert an int (a players coordniate in a game) into a const char so that I can send it using winsock. Here is the code:
ZeroMemory(buffer, 256);
delete [] buffer;

where p1.y_pos is the players y position. I do not know how to convert it into a string/char. I am guessing something like p1.y_pos.c_str() but I dont think that will work. What should I do?

Share this post

Link to post
Share on other sites
Here's the way I would do it, using std::stringstream:

#include <string>
#include <sstream>

std::string IntToString(int value)
std::ostringstream converter;
converter << value;
return converter.str();

This function returns a std::string. To use it in the send function, try something like:

std::string buffer = IntToString(p1.y_pos);
send(connectSocket, buffer.c_str(), buffer.length(), 0);

The above has not been compiled, but should be correct (I hope).

Share this post

Link to post
Share on other sites
you don't need to put it in a string.

just do a type-cast:


send(connectSocket,(const char*)&p1.y_pos,sizeof(p1.y_pos),0);

*edit*: D'OH... missed the 'address of' operator (&)

[Edited by - Madhed on February 9, 2005 5:17:05 PM]

Share this post

Link to post
Share on other sites
If you're using std::string this will work

std::ostringstream strstrm;

Share this post

Link to post
Share on other sites
Thank you all for your help. I used the type casting method I believe it was and it works. I do have a question though, how would I convert back a char to an int? Here is the code

//Recieve p2 y position
p2.y_pos = buffer;
delete [] buffer;

I tried type casting it as:
p2.y_pos = (int*)&buffer;
but it would not work. This is the last help I need I believe. Thank you

Share this post

Link to post
Share on other sites
Original post by Madhed
send(connectSocket,(const char*)&p1.y_pos,strlen(buffer),0);

OK, I'm not familiar with that function, but I'm guessing you would need to do something like this (if it's anything like working with binary files)

send(connectSocket,(const char*)&p1.y_pos,sizeof(p1.y_pos),0)

Share this post

Link to post
Share on other sites
(edit: hum... was the last one...)


1)The C++ way: use a string stream

#include <sstream>
std::stringstream ss;
ss << p1.y_pos << std::ends;
std::string str = ss.str();

2) the C way: use sprintf() (not strcpy)

char buffer[16];
sprintf(buffer, "%d", p1.y_pos);

I added the +1 because I wanted to send the /0 character. If I don't then how would I know where my string ends?

A better solution should be to send the binary data directly:


This is probably easier to manage and to read, and avoid a transformation to a string.


Share this post

Link to post
Share on other sites
All the replies above are good, but if you're not using C++ or the STL, Madhed's reply is fine (although you want sizeof(int) rather than strlen(buffer), assuming y_pos is an int). Using stringstreams might be overkill for what you want.

Thinking of the const char* as a string isn't too helpful because it makes you think of text, but it's just being used here to denote generic data: you can take the address of any data and pass it to send, along with the length of the data.

EDIT: bah, I'm a slow poster :)

Share this post

Link to post
Share on other sites
Okay, for receiving the data you should take these steps:

1. You must know in what order you sent your data.
2. For every send() you do a recv() with the same data type

send(sock,(const char*)&bla,sizeof(int),0);

I know some people might want to kill me for oversimplifying this topic
but that's basically what you will be doing.

When you got it up and running you might want to address the topics of
serializing/deserializing, strucure padding, endianess and the like...

but don't get confused just play around a bit and when you think know
what's going on read about those things related to networking.

EDIT: I'm not sure, but /me thinks that recv() must get a (char*)
pfffft. damn winsock, unix is teh r0xxx0rs ;)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!