0

I want to dump my monitors(I have only one though), and I used this code:

GLFWmonitor** monitors = glfwGetMonitors(&num);
cout << "Monitors dump:" << endl;
for(int i=0;i<num;i++){
    const char* name = glfwGetMonitorName(monitors[i]);
    cout << (string)name << endl;
}

The returned string is all obfuscated:

閫氱敤鍗虫彃鍗崇敤鐩戣鍣

I have a Chinese system, but in my system monitor screen, the name is HP N246v which is DEFINITELY utf-8(no Chinese characters), so I'm thinking, maybe it is utf-8, but encoded in utf-16. Nevertheless, I want glfwGetMonitorName to return properly encoded strings. Also, I seriously want a way to get GLFW to return utf-16 characters. But it's okay(since utf-16 isn't supported well over libraries and codes due to most English text is in utf-8.)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Are you compiling with Unicode support, I doubt if the monitor name is returned in Chinese instead of English! There are times when we process ANSI instead of Unicode you get buffer overflow and resulting in non-English or non-printable characters – Anand Sowmithiran Dec 17 '21 at 13:50
  • 1
    Before printing anything to stdout, call this `SetConsoleOutputCP(CP_UTF8)`, because the console output may not print the UTF-8 strings properly. – Anand Sowmithiran Dec 17 '21 at 13:57
  • 3
    `(string)name` tells the compiler to interpret `name` as if it was a `std::string` instance. Just `cout << name` should suffice. – Botje Dec 17 '21 at 14:06
  • std::cout does not handle UTF-8. Have you tried to convert the monitor name from UTF-8 to UTF-16 and print using std::wcout ? This would at least give you a better idea of what the actual returned name is. – Michaël Roy Dec 17 '21 at 15:35
  • @Michaël Roy std::wcout prints NOTHING to the console. –  Dec 19 '21 at 01:26
  • @Anand Sowmithiran That requires #include which I suppose is Windows-only and will hurt portability since I am going to send the program to a Mac friend. –  Dec 19 '21 at 01:26
  • @Botje that didn't help and it prints the same things. –  Dec 19 '21 at 01:27
  • @Michaël Roy I tried using the boilerplate from the [internet](https://stackoverflow.com/questions/22950412/c-cant-get-wcout-to-print-unicode-and-leave-cout-working), still no luck. –  Dec 19 '21 at 01:33
  • Can you inspect the actual bytes returned by `glfwGetMonitorName` with a debugger instead of printing them to the terminal? Also, statements like "utf-8, but encoded in utf-16" make me think you don't fully grasp the concept and terminology of unicode. – Botje Dec 20 '21 at 10:03
  • @Botje i mean all characters are perfectly within the UTF-8 range(that is, encodeable with UTF-8 after decoded from UTF-16) BUT the bytes returned are UTF-16 code. –  Dec 27 '21 at 09:16
  • Both UTF-8 and UTF-16 can represent the full Unicode range. Your statement does not make any sense to me, sorry. That is why I asked to see the actual bytes. – Botje Dec 27 '21 at 22:56
  • I know there is a "supplementry range" for UTF-8 but i dont think most functions support that –  Jan 10 '22 at 10:39

0 Answers0