Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a const char* and/or const std::string in C++ with a sequence of UTF-8 character?

Tags:

c++

utf-8

How to initialize a const char* and/or const std::string in C++ with a sequence of UTF-8 characters?

I'm using a regular expression API that accepts UTF8 string as const char*. The initialization code should be platform independent.

like image 493
Leonid Avatar asked Oct 07 '10 11:10

Leonid


2 Answers

This should work with any compiler:

const char* twochars = "\xe6\x97\xa5\xd1\x88";
like image 159
Nemanja Trifunovic Avatar answered Sep 27 '22 20:09

Nemanja Trifunovic


Compiler - independent answer is also: Save the file in UTF-8 without BOM signature encoding.

const char* c = "ěščř"; //Just save the file in UTF-8 without BOM signature.

(See the comment of question.)
Btw, Windows console must bee set to UTF8. For many details see post into question.

like image 20
vladasimovic Avatar answered Sep 27 '22 21:09

vladasimovic