3

I use a drawing api which takes in a const char* to a utf-8 encoded string. Doing myStdString.cstr() does not work, the api fails to draw the string.

for example:

sd::stringsomeText = "■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱";

will render ???????????????

So how do I get the std::string to act properly?

Thanks

1
  • 2
    std::string, and it's member functions, should NOT EVER be used for UTF-8! Too many people think that UTF-8 means they can simply use their plain old char without considerations -- they are incorrect. Commented Oct 31, 2010 at 3:07

2 Answers 2

3

Try using std::codecvt_utf8 to write the string to a stringstream and then pass the result (stringstream::str) to the API.

Sign up to request clarification or add additional context in comments.

Comments

3

There are so many variables here it's hard to know where to begin.

First, verify that the API really and truly supports UTF-8 input, and that it doesn't need some special setup or O/S support to do so. In particular make sure it's using a font with full Unicode support.

Your compiler will be responsible for converting the source file into a string. It probably does not do UTF-8 encoding by default, and may not have an option to do so no matter what. In that case you may have to declare the string as a std::wstring and convert it to UTF-8 from there. Alternatively you can look up each character beyond the first 128 and encode them as hex values in the string, but that's a hassle and makes for an unreadable source.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.