ref:c++ example code at bottom from http://msdn.microsoft.com/en-us/library/windows/desktop/ms740120(v=vs.85).aspx I have a char array
char RecvBuf[1024];
which gets filled up on receiving UDP message. How do I store it in a std::string variable like string str;
so that I can return a string from my function?
Use this constructor of std::string
:
string ( const char * s, size_t n );
Basically, if RecvBuf
is a standard (text) "string", it's OK just to make
std::string str( RecvBuf );
but this will copy all char
s, until \\0
is hit.
And as this buffer stores raw bytes, received from a socket, you should expect \\0
bytes.
When you receive the data, you'll know it's size. So, just use
std::string str( RecvBuff, buff_len );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.