int main(int argc, char *argv[])
{
DWORD dwWritten;
// 1st try: replace oem with ascii
#ifdef _WIN32
std::cout << "Switch input to Ascii CodePage (1252): " << (::SetConsoleCP(::GetACP())?"ok":"fail") << std::endl;
std::cout << "Switch output to Ascii CodePage (1252): " << (::SetConsoleOutputCP(::GetACP())?"ok":"fail") << std::endl;
std::cout << "Current input CodePage: " << (unsigned int)::GetConsoleCP() << std::endl;
std::cout << "Current output CodePage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
#endif
qStdOut <<
QString("QTextStream: ") << unicodeString <<
QChar('\n');
qStdOut.flush();
qStdOut.setCodec("UTF-16");
qStdOut <<
QString("QTextStream: ") << unicodeString <<
QChar('\n');
qStdOut.flush();
std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
std::cout << "cout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;
std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;
std::wcout << L"wcout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;
printf("printf: %ls\n", unicodeString.utf16());
wprintf(L"wprintf: %ls\n", unicodeString.utf16());
#ifdef _WIN32
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
#endif
// 2nd try : set CP to utf-16
#ifdef _WIN32
std::cout << "\nSet input CP to ucs2: " << (::SetConsoleCP(1200)?"ok":"fail") << std::endl;
std::cout << "Set output CP to ucs2: " << (::SetConsoleOutputCP(1200)?"ok":"fail") << std::endl;
std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;
std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
#endif
qStdOut <<
QString("QTextStream: ") << unicodeString <<
QChar('\n');
qStdOut.flush();
std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;
printf("printf: %ls\n", unicodeString.utf16());
wprintf(L"wprintf: %ls\n", unicodeString.utf16());
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
// 3rd try : set CP to utf-8
#ifdef _WIN32
std::cout << "\nSet input CP to utf-8: " << (::SetConsoleCP(65001)?"ok":"fail") << std::endl;
std::cout << "Set output CP to utf-8: " << (::SetConsoleOutputCP(65001)?"ok":"fail") << std::endl;
std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;
std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
#endif
qStdOut.setCodec("UTF-8");
qStdOut <<
QString("QTextStream: ") << unicodeString <<
QChar('\n');
qStdOut.flush();
std::cout << "cout: " << (char*)unicodeString.toUtf8().constData() << std::endl;
std::wcout << L"wcout: " << (char*)unicodeString.toUtf8().constData() << std::endl;
printf("printf: %ls\n", unicodeString.toUtf8().constData());
wprintf(L"wprintf: %ls\n", unicodeString.toUtf8().constData());
#ifdef _WIN32
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
#endif
return a.exec();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString unicodeString(QChar(0x9788));
QTextStream qStdOut(stdout, QIODevice::WriteOnly);
DWORD dwWritten;
// 1st try: replace oem with ascii
#ifdef _WIN32
std::cout << "Switch input to Ascii CodePage (1252): " << (::SetConsoleCP(::GetACP())?"ok":"fail") << std::endl;
std::cout << "Switch output to Ascii CodePage (1252): " << (::SetConsoleOutputCP(::GetACP())?"ok":"fail") << std::endl;
std::cout << "Current input CodePage: " << (unsigned int)::GetConsoleCP() << std::endl;
std::cout << "Current output CodePage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
#endif
qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
qStdOut.flush();
qStdOut.setCodec("UTF-16");
qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
qStdOut.flush();
std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
std::cout << "cout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;
std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;
std::wcout << L"wcout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;
printf("printf: %ls\n", unicodeString.utf16());
wprintf(L"wprintf: %ls\n", unicodeString.utf16());
#ifdef _WIN32
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
#endif
// 2nd try : set CP to utf-16
#ifdef _WIN32
std::cout << "\nSet input CP to ucs2: " << (::SetConsoleCP(1200)?"ok":"fail") << std::endl;
std::cout << "Set output CP to ucs2: " << (::SetConsoleOutputCP(1200)?"ok":"fail") << std::endl;
std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;
std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
#endif
qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
qStdOut.flush();
std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;
printf("printf: %ls\n", unicodeString.utf16());
wprintf(L"wprintf: %ls\n", unicodeString.utf16());
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
// 3rd try : set CP to utf-8
#ifdef _WIN32
std::cout << "\nSet input CP to utf-8: " << (::SetConsoleCP(65001)?"ok":"fail") << std::endl;
std::cout << "Set output CP to utf-8: " << (::SetConsoleOutputCP(65001)?"ok":"fail") << std::endl;
std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;
std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
#endif
qStdOut.setCodec("UTF-8");
qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
qStdOut.flush();
std::cout << "cout: " << (char*)unicodeString.toUtf8().constData() << std::endl;
std::wcout << L"wcout: " << (char*)unicodeString.toUtf8().constData() << std::endl;
printf("printf: %ls\n", unicodeString.toUtf8().constData());
wprintf(L"wprintf: %ls\n", unicodeString.toUtf8().constData());
#ifdef _WIN32
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
#endif
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Switch input to Ascii CodePage (1252): ok
Switch output to Ascii CodePage (1252): ok
Current input CodePage: 1252
Current output CodePage: 1252
Q T e x t S t r e a m : ˆ—
cout: ˆ—
cout: 鞈
wcout: printf:
wprintf: WriteConsoleW: 鞈
Set input CP to ucs2: fail
Set output CP to ucs2: fail
Current input codepage: 1252
Current output codepage: 1252
Q T e x t S t r e a m : ˆ—
cout: ˆ—
printf:
wprintf: WriteConsoleW: 鞈
Set input CP to utf-8: ok
Set output CP to utf-8: ok
Current input codepage: 65001
Current output codepage: 65001
cout: printf:
wprintf: WriteConsoleW: 鞈
Switch input to Ascii CodePage (1252): ok
Switch output to Ascii CodePage (1252): ok
Current input CodePage: 1252
Current output CodePage: 1252
QTextStream: ?
Q T e x t S t r e a m : ˆ—
cout: ˆ—
cout: 鞈
wcout: printf:
wprintf: WriteConsoleW: 鞈
Set input CP to ucs2: fail
Set output CP to ucs2: fail
Current input codepage: 1252
Current output codepage: 1252
Q T e x t S t r e a m : ˆ—
cout: ˆ—
printf:
wprintf: WriteConsoleW: 鞈
Set input CP to utf-8: ok
Set output CP to utf-8: ok
Current input codepage: 65001
Current output codepage: 65001
QTextStream: 鞈
cout: printf:
wprintf: WriteConsoleW: 鞈
To copy to clipboard, switch view to plain text mode
Bookmarks