Project

General

Profile

WString and UTF-8 encoding

Added by Radu Marin almost 11 years ago

Hi!

I'm trying to render an UTF-8 encoded text(Romanian characters) using WPdfRenderer with no succes this far. My project uses Multi-Byte Character Set. I've explored a number of possibilities, but nothing worked out.

This is a sample of the text i read from a file "ăîâșț" and the result after converting to Wt::WString "ăîâșț".

I've tried this:

CStringW strW = L"ăîâșț";
std::wstring stdwstr(strW);
Wt::WString wstr(stdwstr);

and this

CStringW strW = L"ăîâșț";
std::wstring stdwstr(strW);
const wchar_t* szStr = stdwstr.c_str();
Wt::WString wstr(szStr)

and setting WString default encoding Wt::WString::setDefaultEncoding(Wt::UTF8);

and also setting the locale setlocale(LC_ALL, "ro_RO.UTF-8"); or setlocale(LC_ALL, "ro_RO.UTF-8");

Does anyone have another suggestion?

Thanks in advance!


Replies (5)

RE: WString and UTF-8 encoding - Added by Radu Marin almost 11 years ago

I started another project, this time using UNICODE char set. The problem persists. I should also add that i use Visual Studio.

RE: WString and UTF-8 encoding - Added by Alan Finley almost 11 years ago

Did you try this:

Wt::WString wstr = Wt::WString::fromUTF8("ăîâșț");

RE: WString and UTF-8 encoding - Added by Radu Marin almost 11 years ago

I've tried Wt::WString wstr = Wt::WString::fromUTF8("ăîâșț"); on a test unicode project, but half of the chars are interpreted as '?'.

Another thing i tried was changing locale from Windows to Romanian. The resulted WString is slightly different, but far from "ăîâșț".

RE: WString and UTF-8 encoding - Added by Alan Finley almost 11 years ago

In my test project I don't set any special encondings and this one works fine:

Wt::WString wstr = Wt::WString::fromUTF8("ăîâșț");
app->root()->addWidget(new Wt::WText(wstr));

RE: WString and UTF-8 encoding - Added by Wim Dumon almost 11 years ago

On Windows, you'll have more success with constructing from wide characters like this:

WString wstr(L"ăîâșț");

But then again, your program may not work if you run it on other language windows versions. I've said it a number of times before in my posts: avoid to write unicode in C source code, it is not portable. Put them in a message resource bundle and use tr() instead.

Best regards,

Wim.

    (1-5/5)