Convert TCHAR array to char array

maxy picture maxy · Nov 24, 2009 · Viewed 39.7k times · Source

How to convert to TCHAR[] to char[] ?

Answer

ozanmuyes picture ozanmuyes · Dec 19, 2012

Honestly, I don't know how to do it with arrays but with pointers, Microsoft provides us with some APIs, such as wctomb and wcstombs. First one is less secure than the second one. So I think you can do what you want to achieve with one array-to-pointer and one pointer-to-array casting like;

// ... your includes
#include <stdlib.h>
// ... your defines
#define MAX_LEN 100
// ... your codes
// I assume there is no any defined TCHAR array to be converted so far, so I'll create one
TCHAR c_wText[MAX_LEN] = _T("Hello world!");
// Now defining the char pointer to be a buffer for wcstomb/wcstombs
char c_szText[MAX_LEN];
wcstombs(c_szText, c_wText, wcslen(c_wText) + 1);
// ... and you're free to use your char array, c_szText

PS: Could not be the best solution but at least it's working and functional.