I came up with this problem when I tried to set the unicode value for a variable: u16string uni = u"\u4F60\u597D";
. When I run this program and watch this variable, it just appeared inproperly.
#include <iostream>
#include <locale>
#include <string>
#include <codecvt>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main()
{
u16string uni = u"\u4F60\u597D"; // unicode "你好"
string mtb = "\344\275\240\345\245\275"; // utf-8 "你好" e4 bd a0 e5 a5 bd
string mtb2 = "你好"; // gbk "你好" c4 e3 ba c3
// gbk: "浣犲ソ" e4 bd a0 e5 a5 bd
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conversion;
std::string mbs = conversion.to_bytes(uni.c_str()); // ni hao (你好)
cout << mtb << endl;
cout << mbs << endl;
cout << mtb2 << endl;
return 0;
}
However, if I use the multibyte form to value a variable: string mtb = "\344\275\240\345\245\275";
It appears as normal.
Can someone tell me how to fix this issue? Thanks.