469,923 Members | 1,633 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Switching from Multi-Byte to Unicode character sets

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
Oct 10 '08 #1
5 1447
Hi jc!
char reply[256] = _T("olleh");
You should get used to TCHAR:

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

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Oct 10 '08 #2
jc wrote:
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.

--
David Wilkinson
Visual C++ MVP
Oct 10 '08 #3

"jc" <jc@discussions.microsoft.comha scritto nel messaggio
news:08**********************************@microsof t.com...
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
Oct 10 '08 #4
In addition to the other replies, here's some more info on using the generic
text mappings...

Generic-Text Mappings in Tchar.h
http://msdn.microsoft.com/en-us/libr...21(VS.80).aspx
Mark

--
Mark Salsbery
Microsoft MVP - Visual C++
Oct 10 '08 #5
"Jochen Kalmbach [MVP]" wrote:
Hi jc!
char reply[256] = _T("olleh");

You should get used to TCHAR:

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

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Hi Jochen,

Thanks for replying to my post. Yes, the TCHAR worked, and the other
conversions throughout the program were not that painful !!
-jc
Oct 11 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dennis Wheeler | last post: by
2 posts views Thread by Frank Swarbrick | last post: by
7 posts views Thread by don | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.