0

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());
Mat
  • 202,337
  • 40
  • 393
  • 406
user3443063
  • 1,455
  • 4
  • 23
  • 37
  • 1
    Use `%c` instead of converting to_wstring ? Or can `aa` be outside of 0-255? – Dave S Jul 24 '23 at 17:29
  • 7
    Why are you using `wprintf` instead of `std::wcout`? – PaulMcKenzie Jul 24 '23 at 17:30
  • 2
    What exactly you are trying to achieve. Form of your code is strange suggesting that you trimmed question to much. – Marek R Jul 24 '23 at 17:33
  • Since wsprintf expects wide strings use %s instead, use %ls for printf – AndersK Jul 24 '23 at 17:36
  • If you use C++20 use [std::wformat](https://en.cppreference.com/w/cpp/utility/format/format) and std::wcout, or C++23 std::print. But I really would recommend not to use printf or wprintf anymore for safety reasons – Pepijn Kramer Jul 24 '23 at 18:37

3 Answers3

3

If you want to convert an int to a single-character std::wstring, you can just use std::wstring's constructor instead of std::to_wstring:

#include <iostream>
#include <string>


int main() {
    int aa = 65;
    std::wstring ws65 = std::wstring(1, aa);
    std::wcout <<  L"   ws65 = " << ws65 << L'\n';
}

which outputs

ws65 = A
Brian61354270
  • 8,690
  • 4
  • 21
  • 43
3

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;
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I suspect `wchar_t` was not fixed in copy paste. – Marek R Jul 24 '23 at 17:38
  • They want `A` to be printed, not `65`. – HolyBlackCat Jul 24 '23 at 17:39
  • 1
    In `wprintf()`, `%c` expects `wchar_t` not `int`. Need to use `%lc` to accept `int` and convert it to `wchar_t`, otherwise just cast the `int` to `wchar_t` before passing it in. – Remy Lebeau Jul 24 '23 at 21:22
  • @RemyLebeau: On what platform does that matter? On x86-64 with Apple clang 14.0.0 it builds and runs as I wrote it, even with `-Wall -Wextra`. – John Zwinck Jul 26 '23 at 18:48
  • @JohnZwinck `wchar_t != int`, and `sizeof(wchar_t) != sizeof(int)` on every platform. – Remy Lebeau Jul 26 '23 at 22:01
  • @RemyLebeau You are wrong, here is proof: https://godbolt.org/z/63Gf8Y7hz . As for %c vs %lc, it does not matter because `65` converts to `65` with `%c` just fine. – John Zwinck Jul 27 '23 at 16:58
  • @JohnZwinck I said they are not the same on *every* platform. Your "proof" is only looking at 1 specific compiler on 1 specific platform. `sizeof(wchar_t)` is `2` on every Windows compiler, for instance, because Microsoft uses `wchar_t` for UTF-16 (and UCS-2 before that), whereas just about every other platform uses `wchar_t` for UTF-32 instead. – Remy Lebeau Jul 27 '23 at 17:05
  • @RemyLebeau: Your phrasing was very ambiguous. I understand what you're saying now. I don't promise that the code I wrote in my answer is portable to all platforms. – John Zwinck Jul 27 '23 at 17:13
1

std::to_wstring() just converts the input into the actual written out number (ie: 123 becomes "123"). printf() and wprintf() are nice because they look at the data you give it in whatever way you specify, so since the actual binary for 'A' and the number 65 are the same, you can just tell it to read the data as a character, no conversions necessary:

int aa=65;
wprintf(L"   ws65 = %lc \n", aa);
Tiger Ros
  • 31
  • 3