Sending TCHAR buffer with send(sock, wszBuffer, ...)?
I have a wide-character XML message that I need to send over a Win32 socket in C++.
TCHAR wszBuffer[1024];
Should I sprintf(sz开发者_运维知识库SendBuffer, "%S", wszBuffer)
the wide character buffer to a char
array before sending it?
What is the correct way to send this?
Pass wszBuffer directly to the socket function, casting it to char*, with length = sizeof(wszBuffer), or 1024 * sizeof(TCHAR). In this case the buffer will be send as is.
If you want to send this text as ANSI string, convert it by any string convertion function. sprintf is OK, another way is W2A. But some information may be lost.
Since you are dealing with XML, you need to encode it in UTF-8 or other XML-friendly character encoding (and specify that encoding in the XML's prolog) before sending it over the socket.
Use WideCharToMultiByte
to convert it to UTF-8 (or any other char
-based encoding, as long as you declare it in the XML file).
You can simply cast it to a char*. On the other end you can simply cast the recieved buffer to wchar_t*.
精彩评论