Converting an int to a const char*

Started by
24 comments, last by Deyja 19 years, 2 months ago
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);
strcpy(buffer,p1.y_pos);
send(connectSocket,buffer,strlen(buffer),0);
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?
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Advertisement
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).
you don't need to put it in a string.

just do a type-cast:

i.e:

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]
If you're using std::string this will work

std::ostringstream strstrm;strstrm<<p1.y_pos;send(connectSocket,strstrm.str(),strstrm.str().length(),0);
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 positionrecv(connectSocket,buffer,256,0);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
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Quote: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)
(edit: hum... was the last one...)

Hello,

1)The C++ way: use a string stream
#include <sstream>std::stringstream ss;ss << p1.y_pos << std::ends;std::string str = ss.str();send(connectSocket,str.c_str(),str.length()+1,0); 


2) the C way: use sprintf() (not strcpy)
char buffer[16];sprintf(buffer, "%d", p1.y_pos);send(connectSocket,buffer,strlen(buffer)+1,0); 

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:
send(connectSocket,&p1.y_pos,sizeof(p1.y_pos),0); 

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

HTH,
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 :)
ah well, that was y typo... my fault =)))))

of course use sizeof(pl.y_pos);

Seee ya
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);
................
recv(sock2,(char*)&bla2,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 ;)

This topic is closed to new replies.

Advertisement