Jump to content
  • Advertisement
Sign in to follow this  
Markie

MS Visual Studio / Windows Unicode Character Weirdness

This topic is 2811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all

I have a problem with Visual Studio 2005 and Windows.
Perhaps someone (probably someone outside the US) could confirm this problem:

When I compile a C++ program and try to display a non-US UNICODE character, such as a german "ä" or "ü", etc. Windows XP seems unable to display such characters, but only when UNICODE is ENABLED!!!
Isn't that weirder than hell?
I'm talking about characters ONLY, not about strings (not C++ strings and not C-style zero-terminated strings). You know the type that uses only one single char or wchar_t and should be displayable with the C-style format specifier %c in printf(), sprintf(), etc.

When I set the character encoding of my project to "None specified" or "Multi-Byte", just anything BUT "Unicode", german "ä", "ö", etc. are displayed fine. But as soon as I set the properties of the project to "Unicode", I get just weird signs like the famous empty box.

Here's the short code snippet that does this weirdness.
Can someone please confirm this and does someone know what's amiss here:


#include <Tchar.h>


// _stprintf test:

TCHAR buffer[200];
TCHAR c = 'ä';
TCHAR s[] = TEXT("computer");
int i = 35, j;
float fp = 1.7320534f;

// Format and print various data:
j = _stprintf( buffer, TEXT(" String: %s\n"), s );
j += _stprintf( buffer + j, TEXT(" Character: %c\n"), c );
j += _stprintf( buffer + j, TEXT(" Integer: %d\n"), i );
j += _stprintf( buffer + j, TEXT(" Real: %f\n"), fp );
j += _stprintf( buffer + j, TEXT("Unicode characters in string: ä ö ü \n"), fp );


MessageBox(NULL, buffer, TEXT("_stprintf test - MessageBox"), 0);

// END: _stprintf test




Here's my output:


OUTPUT in Message Box when compiled WITH UNICODE:
------------------------
String: computer
Character: "BOX" (unable to display this character!)
Integer: 35
Real: 1.732053
Unicode characters in string: ä ö ü
------------------------

OUTPUT in Message Box when compiled WITHOUT UNICODE:
------------------------
String: computer
Character: ä
Integer: 35
Real: 1.732053
Unicode characters in string: ä ö ü
------------------------


Any help appreciated!
:-(
Mark

Share this post


Link to post
Share on other sites
Advertisement
My native language does not even use the Latin alphabet, so I know your pain :)

This looks like a perfect font/encoding mismatch. In my experience this means that:
  • Your Windows font supports one of the Latin encodings, but not Unicode (unlikely)
  • The .c file is encoded differently than the Windows setting. (much more likely)Hope this helps.

    PS:
    I always use the default setting in VS and I am always able to display anything my fonts support.
    You don't need to use the TCHAR stuff unless you are going to support an ansi build of Windows. As far as I recall, all NT-s after 2000 are unicode builds.

Share this post


Link to post
Share on other sites

Hello! When Unicode is enabled, try this line:

TCHAR c = 'ä';

this way:

TCHAR c = L'ä';

Hope it helps.


SOLVED!!! :-)))
THANKS!! :-)
That was it!!
I was wondering if it might be something like that, but then my C book didn't mention ANY of that when introducing the character data type. Not even in a foot note. Instead, it doesn't mention any of that until an appendix. Of course, another case to take an ink pen to a programming book!! :-)

One should not only use the "L" prefix or Window's TEXT("") macro for wide character constants just like for strings, but one should also use the printf specifiers %lc and %ls instead of the normal character specifiers %c and %s for character constants and strings!

This should work now:
#include <Tchar.h>

// _stprintf test:

TCHAR buffer[200];
TCHAR c = TEXT('ä'); // Or, same as: c = L'ä';
TCHAR s[] = TEXT("computer"); // Or, same as: c = L"computer";
int i = 35, j;
float fp = 1.7320534f;

// Format and print various data:
j = _stprintf( buffer, TEXT(" String: %ls\n"), s ); // sometimes %s works too, but not recommended
j += _stprintf( buffer + j, TEXT(" Character: %lc\n"), c ); // sometimes %c works too, but not recommended
j += _stprintf( buffer + j, TEXT(" Integer: %d\n"), i );
j += _stprintf( buffer + j, TEXT(" Real: %f\n"), fp );
j += _stprintf( buffer + j, TEXT("Unicode characters in string: ä ö ü \n"), fp );


MessageBox(NULL, buffer, TEXT("_stprintf test - MessageBox"), 0);

// END: _stprintf test


Thanks a lot for the help!
:-))

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!