Re: Switching from Multi-Byte to Unicode character sets
- From: "Giovanni Dicanio" <giovanniDOTdicanio@xxxxxxxxxxxxxxxxx>
- Date: Fri, 10 Oct 2008 19:15:07 +0200
"jc" <jc@xxxxxxxxxxxxxxxxxxxxxxxxx> ha scritto nel messaggio
news:08230DC5-0A9E-468F-AE81-0054ED8ED0B4@xxxxxxxxxxxxxxxx
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
.
- References:
- Prev by Date: Re: Switching from Multi-Byte to Unicode character sets
- Next by Date: Re: Switching from Multi-Byte to Unicode character sets
- Previous by thread: Re: Switching from Multi-Byte to Unicode character sets
- Next by thread: Re: Switching from Multi-Byte to Unicode character sets
- Index(es):
Relevant Pages
|