Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert int to const wchar_t*

Tags:

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);
like image 801
Kira Avatar asked Feb 27 '13 10:02

Kira


2 Answers

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.

like image 92
Mr.C64 Avatar answered Sep 30 '22 20:09

Mr.C64


In C++11:

wstring value = to_wstring(100);

Pre-C++11:

wostringstream wss;
wss << 100;
wstring value = wss.str();
like image 37
Peter Wood Avatar answered Sep 30 '22 19:09

Peter Wood