문제

I have a wide-character XML message that I need to send over a Win32 socket in C++.

TCHAR wszBuffer[1024];

Should I sprintf(szSendBuffer, "%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*.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top