Re: Switching from Multi-Byte to Unicode character sets

Tech-Archive recommends: Fix windows errors by optimizing your registry




"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


.



Relevant Pages

  • Re: VC2005 Pro: IDE (Compiler ?) cant find Stdafx.h
    ... For new code I would obviously use UNICODE but for existing stuff ... > Use Multi-Byte Character Set ... > If you use TCHAR etc., and want your code to run on another platform, then ... > typedef char TCHAR; ...
    (microsoft.public.vc.language)
  • Re: passing char * to dll
    ... char is a single byte thing so there are only 256 different possible values. ... This makes it difficult to have fonts of more than 256 characters. ... Unicode is a way of having all possible characters (latin, chinese, arabic, ... The idea was that you write your code using TCHAR everywhere and TCHAR is ...
    (microsoft.public.vc.mfc)
  • Re: Unicode and ANSI strings in the same project
    ... If you don't there will be all sorts of problems passing CStrings, TCHAR and TCHAR *, variables from a function in one file to one in another. ... I changed most of my char variables to TCHAR and all the associated stuff like strlento _tcslen. ... In some places I used WCHAR explicitly etc) as I changed my document file format to contain unicode strings before I changed the program to use unicode everywhere. ...
    (microsoft.public.vc.language)
  • Re: passing char * to dll
    ... char is a single byte thing so there are only 256 different possible values. ... This makes it difficult to have fonts of more than 256 characters. ... Unicode is a way of having all possible characters in a single set. ... To make the transition as painless as possible Microsoft invented the transitional type TCHAR. ...
    (microsoft.public.vc.mfc)
  • Re: How to LPCTSTR Convert to char *
    ... number of people who use 'char' because they've never grown beyond their first programming ... These are the people who are getting nuked by VS2005 which defaults to Unicode apps. ... isolated to the embedded interface (rare and exotic situation imposed by external ... fields with char strings is quite essential. ...
    (microsoft.public.vc.mfc)