Search code examples
c++type-conversionprintfwstringwchar

C++ How to wprintf an int as wstring


I have the following code. I want it to print the letter A but it gives me 65.

How do I wprintf the wchar/wstring of this number?

  int aa=65;
  std::wstring ws65 = std::to_wstring(aa);
  wprintf(L"   ws65 = %ls \n", ws65.c_str());

Solution

  • You don't need to convert to string first, just do this:

    int aa=65;
    wprintf(L"   ws65 = %c \n", aa);
    

    That's much more efficient, as you avoid an extra string allocation, construction, and copy.

    Or if you want to use wcout, which is the "more C++" way:

    std::wcout <<  L"   ws65 = " << static_cast<wchar_t>(aa) << std::endl;