I am writing a library for the printer. The input is text in UTF-8 format. The printer prints Cyrillic in CP866 (Cyrillic) format. How to convert UTF-8 to CP866? Perhaps there are special features?

  • 3
    If it is under Windows, convert to UTF-16 using MultiByteToWideChar, and then to CP-866 using WideCharToMultiByte. - VladD
  • Yes, I saw such a decision, but something did not work out at all. If it's not hard for you, could you give a simple example? - Madisson
  • I will write an example from the phone when I get to the computer (if no one else writes in the meantime). - VladD
  • 1. Are there characters in the input stream that are not supported by the CP866? 2. dwFlags in MultiByteToWideChar specified (if yes, then the function will not work for UTF-8)? 3. WC_ERR_INVALID_CHARS not found anywhere? - ߊߚߤߘ
  • 1. No, purely Russian. 2. dwFlags = 0. But in fact, I think I’m just doing something wrong. That's why it doesn't work. I am new to c ++ (this is my second program). For this I need a simple example. - Madisson

1 answer 1

 ifstream file("text.txt"); char pszCode[30]; file.getline(pszCode, 30); BSTR bstrWide; char pszAnsi[50]; int nLength; char* text = ""; nLength = MultiByteToWideChar(CP_UTF8, 0, pszCode, strlen(pszCode) + 1, NULL, NULL); bstrWide = SysAllocStringLen(NULL, nLength); MultiByteToWideChar(CP_UTF8, 0, pszCode, strlen(pszCode) + 1, bstrWide, nLength); nLength = WideCharToMultiByte(CP_OEMCP, 0, bstrWide, -1, NULL, 0, NULL, NULL); WideCharToMultiByte(CP_OEMCP, 0, bstrWide, -1, pszAnsi, nLength, NULL, NULL); SysFreeString(bstrWide); cout << pszAnsi << endl; 

In general, I used this method, as VladD advised, in principle everything works, although of course I did not understand how these two functions work to the end. But thanks anyway!