How to get decoded String from UTF16 encoded String

Viewed 98

I'm trying to get decoded String from UTF16 encoded String.

Current value of UTF16 is \u5f00\u53d1\u8005 and original string is 开发者 In other words, \u5f00\u53d1\u8005 ==> 开发者 using C++`.

How can I do this using C++?

1 Answers

Assuming you have it stored in a wchar_t array or std::string it's just a matter of correctly setting your environment to deal with wide chars, take this console example:

#include <iostream>
#include <fcntl.h>

int main(){

  _setmode(fileno(stdout), _O_WTEXT);
  const wchar_t str[] = L"\u5f00\u53d1\u8005";
  //std::wstring str = L"\u5f00\u53d1\u8005";
  std::wcout << str;
    
}

It's as simple as that.

To use with mfc particularly maybe you should take a look at MultiByteToWideChar and/or WideCharToMultiByte.

There is also this post in Code Project that may have just what you need.

Related