Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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());

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading