How to Convert int to char*(string) in C++?
-
SOCKET lhSocket; int iResult; lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); char *sendbuf = "this is a test"; iResult = send(lhSocket, sendbuf, (int)strlen(sendbuf), 0 ); printf("Bytes Sent: %ld\n", iResult); I have client and Server program using sockets in C++ now i send a buffer it is received by server now when server acknowledge me back saying i got your packet i should get that in string format not bytes received : something. how to achieve that ? My iresult returns me an integer value, I am sending a message over socket to server , i dont want to print it as Bytes sent : 14. I want to print the message sent as string to server. I am dealing with Sockets. How i can achieve this in C++
-
Answer:
sendbuf is the string which you are sending. Print sendbuf instead: printf("Bytes Sent: %s\n", sendbuf);
Swapnil Gupta at Stack Overflow Visit the source
Other answers
stringstream buf; buf << 12345; buf.str(); // string("12345") buf.str().c_str(); // char* "12345"
Tomasz Wysocki
Take a look at this: http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
Markos
You're asking different things in the title and your post. Converting int to string in C++ is done with #include <sstream> std::ostringstream oss; oss << some_int; // do whatever with oss.str()... as Tomasz illustrated. To receive data from a socket, you need to make a further call to either recv() or read(). Your "send" call does not itself wait for the reply. recv() or read() accept character-array buffers to read the response into, but you will need to loop reading however much the calls return until you have enough of a response to process, as TCP is what's called a "byte stream" protocol, which means you are not guaranteed to get a complete packet, line, message or anything other than a byte per call. Given the level of understanding your question implies, I strongly suggest you have a look at the GNU libC examples of sockets programming - there are some server and client examples - easily found via Google.
Tony Delroy
Another opportunity is boost::lexical_cast<> const int myIntValue = 12345; const std::string myStringValue = boost::lexical_cast(myIntValue);
fbasile
if you use visual C++ 2008 or 2010 i think there is a function inbuilt to do your job. something like itoa(int); will convert the given int and return a char * pretty simple its in stdlib.hlbtw ** make sure this is not the same in all compilers or distrubutions http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/ here is a link for reference
Abhishek
Related Q & A:
- How to Convert Json date string to more readable date format?Best solution by SharePoint
- How to Convert a C++ Structure into C# Structure?Best solution by Stack Overflow
- How to split a string in C++?Best solution by Stack Overflow
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- How to marshall a string in C?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.