Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert char* to wchar_t* using mbstowcs_s

I've tried convert a char* to wchar_t*, but I'm having some trouble using the mbstowcs and Visual Studio wants mbstowcs_s...

char *port; 
size_t size = strlen(port) + 1;  
wchar_t* portName = new wchar_t[size]; 
mbstowcs(portName, port, size);

How can I change the function to mbstowcs_s?

like image 451
Lucas Martins Avatar asked Apr 24 '15 12:04

Lucas Martins


1 Answers

I wouldn't recommend disabling the secure code warnings when the fix to use the secure methods is so easy, so here you go:

    const char *port="8080";
    size_t size = strlen(port) + 1;  
    wchar_t* portName = new wchar_t[size]; 

    size_t outSize;
    mbstowcs_s(&outSize, portName, size, port, size-1);

    std::wcout << portName << std::endl;

Tested with cl /W3 /EHsc on VS2013.

like image 67
Andy Brown Avatar answered Oct 19 '22 06:10

Andy Brown