user301649
user301649

Reputation:

How to convert an ASCII string to an UTF8 string in C++?

How to convert an ASCII std::string to an UTF8 (Unicode) std::string in C++?

Upvotes: 3

Views: 7289

Answers (3)

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247969

std::string ASCIIToUTF8(std::string str) {
  return str;
}

Every ASCII character has the same representation in UTF8, so there is nothing to convert. Of course, if the input string uses an extended (8-bit) ASCII character set, the answer is more complex.

Upvotes: 6

CB Bailey
CB Bailey

Reputation: 791869

ASCII is a seven-bit encoding and maps identically onto the UTF-8 encoding of the subset of characters that can be represented in ASCII.

In short, there is nothing to do. Your ASCII string is already valid UTF-8.

Upvotes: 2

Tronic
Tronic

Reputation: 10430

I assume that by ASCII you mean CP1252 or other 8 bit character set (ASCII is only 7 bits and it is directly compatible with UTF-8, no conversion required). Standard C++ cannot do it. You need e.g. Glibmm, Qt, iconv or WINAPI to do it.

Upvotes: 0

Related Questions