Jump to content
  • Advertisement
Sign in to follow this  
Tree Penguin

Hard coded unicode characters? C++

This topic is 3633 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, is it possible to define strings containing unicode characters in the code itself, as in: wchar_t text = "こにちわ"; I searched a lot but everyone seems to use L"Text here." to convert the 8-bit characters into wide ones, which obviously doesn't work for non-ascii characters. Is there a way? I can't imagine all japanese, chinese, korean, arabic, etc. developers need to load every single word from a file. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
wchar_t* text = L"こにちわ";

which gamedev interestingly interprets as:

wchar_t* text = L"こにちわ";


Quote:
to convert the 8-bit characters into wide ones, which obviously doesn't work for non-ascii characters.


Yes it does. It doesn't work if your text editor only supports ascii. It's fine in visual C++ for example....

Share this post


Link to post
Share on other sites
I just tested it with the Visual Studio 2005 compiler.

You still need to specify that the string is Wide Character by useing the _T macro or by prepending the string with an L

TCHAR * str = _T("こにちわ");


And you need to save your source file in a unicode friendly manner

I tried the codepages:

Unicode - Codepage 1200
Unicode (UTF-8 with signature) - Codepage 65001

And met with success.

Share this post


Link to post
Share on other sites
L"text" does not convert (e.g., there's no runtime overhead); L"text" is a wchar_t string literal. That's what you want to use. The problems you may have run into with that are likely related to the encoding your save your file in, and how your compiler or IDE copes with that.

Share this post


Link to post
Share on other sites
Some compilers will accept UTF-8 or UTF-16 encoded source files. In that case you can hardcode characters like that. An alternative is to encode the string using escapes like so:

wstring str = L"\x3053\x306B\x3061\x308F";

Share this post


Link to post
Share on other sites
I got it, I think.

_T or L didn't seem to work because I was testing the strings with SetWindowText(W), as the titlebar of the main window is the only unicode-viewing part of the project at the moment. But that displayed the wrong characters (kanji or kanji-like characters).

I just tried _T again and checked the debugger to make sure the string was stored in memory correctly, and it is, so the problem is with unicode and SetWindowText.

As long as I know the memory is ok I can work with it.

Thanks everyone.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!