code snippet ( char* string to wchar_t* string )
inline std::unique_ptr<wchar_t[]> to_wchar_ts(const char * orig)
{
// newsize describes the length of the
// wchar_t string called wcstring in terms of the number
// of wide characters, not the number of bytes.
size_t newsize = strlen(orig) + 1;
// The following creates a buffer large enough to contain
// the exact number of characters in the original string
// in the new format. If you want to add more characters
// to the end of the string, increase the value of newsize
// to increase the size of the buffer.
std::unique_ptr<wchar_t[]> wcstring(new wchar_t[newsize]);
// Convert char* string to a wchar_t* string.
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring.get(), newsize, orig, _TRUNCATE);
return wcstring;
}
code snippet ( wchar_t* string to char* string )
inline std::unique_ptr<char[]> to_chars(wchar_t *orig)
{
// Convert the wchar_t string to a char* string. Record
// the length of the original string and add 1 to it to
// account for the terminating null character.
size_t origsize = wcslen(orig) + 1;
// Allocate two bytes in the multibyte output string for every wide
// character in the input string (including a wide character
// null). Because a multibyte character can be one or two bytes,
// you should allot two bytes for each character. Having extra
// space for the new string is not an error, but having
// insufficient space is a potential security problem.
const size_t newsize = origsize * 2;
// The new string will contain a converted copy of the original
// string.
std::unique_ptr<char[]> nstring(new char[newsize]);
// Put a copy of the converted string into nstring
size_t convertedChars = 0;
wcstombs_s(&convertedChars, nstring.get(), newsize, orig, _TRUNCATE);
return nstring;
}
From :How to: Convert Between Various String Types