Switching from Multi-Byte to Unicode character sets

  • Thread starter Thread starter jc
  • Start date Start date
J

jc

Hello,
This compiles OK using Multi-Byte character set,
but when I switch to Unicode I get an error.
char reply[256] = _T("olleh");

I know this will fix the error with the Unicode compile,
wchar_t reply[256] = _T("olleh");
but this causes many other conversion problems in the program.

Is there some other conversion macro that I could use instead of _T(),
instead of having to change from char to a wchar_t?

Thanks,
-JC
 
jc said:
Hello,
This compiles OK using Multi-Byte character set,
but when I switch to Unicode I get an error.
char reply[256] = _T("olleh");

I know this will fix the error with the Unicode compile,
wchar_t reply[256] = _T("olleh");
but this causes many other conversion problems in the program.

Is there some other conversion macro that I could use instead of _T(),
instead of having to change from char to a wchar_t?

JC:

You have to write

TCHAR reply[256] = _T("olleh");

if you still want to compile for multi-byte,

or

wchar_t reply[256] = L"olleh";

if you only need to compile for Unicode.

If these cause problems for your code, these problems need to be fixed.

You would not be having this problem if you had used TCHAR, _T("") etc originally.
 
jc said:
Hello,
This compiles OK using Multi-Byte character set,
but when I switch to Unicode I get an error.
char reply[256] = _T("olleh");

Because when you switch to Unicode, _T("...") macro expands to L"...", so
your code becomes:

char reply[ 256 ] = L"olleh"

which is wrong (because the L"olleh" is a wchar_t string, not a char
string).

I know this will fix the error with the Unicode compile,
wchar_t reply[256] = _T("olleh");
but this causes many other conversion problems in the program.

The correct code should be:

TCHAR reply[ 256 ] = _T("olleh");

TCHAR will expands to wchar_t in Unicode builds, and to char in ANSI/MBCS
builds.

BTW: why don't you use CString instead of raw TCHAR arrays?

Is there some other conversion macro that I could use instead of _T(),
instead of having to change from char to a wchar_t?

If you want to convert from generic TCHAR to char, you may want to use ATL
helper classes, like CT2A:

CT2A reply( _T("olleh") );

Both in Unicode and in ANSI/MBCS builds, your 'reply' variable is a RAII
buffer made by char's.

More information here:

http://msdn.microsoft.com/en-us/library/87zae4a3.aspx

HTH,
Giovanni
 
Back
Top