• Advertisement
Sign in to follow this  

MultiByteToWideChar failing to convert 0xf6 with CP_UTF8

This topic is 2509 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to convert a string encoded using UTF8 to a wstring, I am using MultiByteToWideChar to achieve this, below is the code (error checking removed for brevity):

#include <Windows.h>
#include <vector>
#include <string>

int main(int argc, char* argv[])
const char str[7] = {
0x52, // 'R'
0xf6, // 'ö'
0x73, // 's'
0x6c, // 'l'
0x65, // 'e'
0x72, // 'r'

// get required length and generate temp buffer
int len = MultiByteToWideChar(CP_UTF8, 0, str, ARRAYSIZE(str), NULL, 0);
std::vector<wchar_t> buffer(len);

// convert from utf8 to wstring
MultiByteToWideChar(CP_UTF8, 0, str, ARRAYSIZE(str), &buffer[0], len);
std::wstring result(buffer.begin(), buffer.end());


However the length returned from the first call is 6, and the second call omits the ö character.
Does anyone have any ideas what could be going wrong here? Is the input string valid UTF8?
Any ideas would be greatly appreciated.

EDIT: I see here that 'ö' should be represented in UTF8 with [font=verdana, arial, helvetica, code2000, sans-serif][size=2]0xC3 0xB6 so I think I need to check the data.[/font]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement