CInternetSession read unicode

Flaviu_ 971 Reputation points
2023-11-10T10:31:25.2633333+00:00

The project is MFC/Unicode (Use Unicode Character Set). Here is a little code that get strings from an address:

	CString sOutput;
	CInternetSession session{};

	char buffer[256]; 
	CStdioFile file = session.OpenURL(address, 1, INTERNET_FLAG_TRANSFER_ASCII);
	while (UINT nRead = file->Read(buffer, sizeof(buffer - 1)))
	{
		buffer[nRead] = '\0';
		sOutput += buffer;
	}

The problem is that I got non-unicode string, even if CString is UNICODE (CStringW). Example:

înțelege

which is non UNICODE. If I change char to wchar_t, I got even worst:

㼼浸敶쳌쳌쳌쳌獲潩㵮

How can adapt the code to read UNICODE?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,683 questions
{count} vote

Accepted answer
  1. RLWA32 44,931 Reputation points
    2023-11-10T12:25:55.8933333+00:00

    You mean byte by byte, at this point?
    sOutput += buffer;

    I mean that you convert the entire buffer to utf-16le BEFORE that statement. Once you have obtained a utf-16le encoded string you should be able to work with that and CString without problems.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.