jmasterx
jmasterx

Reputation: 54173

utf-8 encoding a std::string?

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

Upvotes: 3

Views: 11525

Answers (2)

Marcelo Cantos
Marcelo Cantos

Reputation: 186058

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

Upvotes: 3

Mark Ransom
Mark Ransom

Reputation: 308520

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.

Upvotes: 3

Related Questions