Kira
Kira

Reputation: 1207

Convert int to const wchar_t*

As the title indicates I want to know the best way to convert an int to a const wchar_t*. in fact I want to use the _tcscpy function

_tcscpy(m_reportFileName, myIntValue);

Upvotes: 22

Views: 40168

Answers (4)

RajSanpui
RajSanpui

Reputation: 12064

If using wstring, might consider the below:

  int ValInt = 20;
  TCHAR szSize[20];

_stprintf(szSize, _T("%d"), ValInt);

wstring myWchar;
myWchar = (wstring)szSize;

Upvotes: 0

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

In C++11:

wstring value = to_wstring(100);

Pre-C++11:

wostringstream wss;
wss << 100;
wstring value = wss.str();

Upvotes: 9

Mr.C64
Mr.C64

Reputation: 42964

Since you are using a C approach (I'm assuming that m_reportFileName is a raw C wchar_t array), you may want to consider just swprintf_s() directly:

#include <stdio.h>  // for swprintf_s, wprintf

int main()
{
    int myIntValue = 20;
    wchar_t m_reportFileName[256];

    swprintf_s(m_reportFileName, L"%d", myIntValue);

    wprintf(L"%s\n", m_reportFileName);
}

In a more modern C++ approach, you may consider using std::wstring instead of the raw wchar_t array and std::to_wstring for the conversion.

Upvotes: 12

unwind
unwind

Reputation: 399881

That's not a "conversion". It's a two-step process:

  1. Format the number into a string, using wide characters.
  2. Use the address of the string in the call.

The first thing can be accomplished using e.g. StringCbPrintf(), assuming you're building with wide characters enabled.

Of course, you can opt to format the string straight into m_reportFileName, removing the need to do the copy altogether.

Upvotes: 2

Related Questions