简体   繁体   中英

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

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.

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 eg 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.

In C++11 :

wstring value = to_wstring(100);

Pre- C++11 :

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

If using wstring, might consider the below:

  int ValInt = 20;
  TCHAR szSize[20];

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

wstring myWchar;
myWchar = (wstring)szSize;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM