2

I am trying to convert an TCHAR* variable to a char* variable. I am doing this because it is a must, and also because I am curious as to how this conversion would be done. I would really appreciate if you could help me. I am an c++ beginner.

Thanks .

Karthik T
  • 31,456
  • 5
  • 68
  • 87
Ionut Daniel
  • 312
  • 2
  • 9
  • 20
  • I assume you are using Win32 APIs. Is that correct? – Asha Dec 07 '12 at 09:23
  • [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Some programmer dude Dec 07 '12 at 09:35
  • [maybe this question](http://stackoverflow.com/questions/1721731/how-to-convert-tchar-pointer-to-char-pointer) or [this question](http://stackoverflow.com/questions/1613217/how-do-i-convert-from-tchar-to-char-when-using-c-variable-length-args) – Pavenhimself Dec 07 '12 at 09:39

1 Answers1

1

A TCHAR is defined depending on your project settings. If your project is using multi-byte, it is already a char. If it's unicode, you would use WideCharToMultiByte to convert. You can do #ifdef UNICODE to check if your project is multi-byte or unicode. i.e:

#ifdef UNICODE
// TCHAR is unicode, convert to char
WideCharToMultiByte(...)
#else
// TCHAR is already char, do nothing
#endif
parrowdice
  • 1,902
  • 15
  • 24
  • Which, of course, leads to unreadable code. This is a good example of why using `TCHAR` is such a bad idea. You need different code to handle `wchar_t` and `char`. Just changing the type isn't sufficient. – James Kanze Dec 07 '12 at 12:12