Convert unicode to char

How to convert Unicode string to char*or char* constin embarcadero C ++?

+1
source share
4 answers

The "Unicode string" is really not specific enough to know what your source data is, but you probably mean the "UTF-16 string stored as a wchar_t array", since most people who don’t know the correct terminology use .

"char *" is also not enough to know what you want to target, although perhaps embarcadero has some conventions. I just assume that you want UTF-8 data unless otherwise indicated.

, VS2010

// your "Unicode" string
wchar_t const * utf16_string = L"Hello, World!";

// #include <codecvt>
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> convert;

std::string utf8_string = convert.to_bytes(utf16_string);

, wchar_t UTF-16, Windows, .

+2
String text = "Hello world";
char *txt = AnsiString(text).c_str();

Older text.t_str() is now AnsiString(String).c_str()
+4

char . , Unicode 4- ,

char32_t data[100];

char:

char const * p = reinterpret_cast<char const*>(data);

for (std::size_t i = 0; i != sizeof data; ++i)
{
    std::printf("Byte %03zu is 0x%02X.\n", i, p[i]);
}

, Unicode.

(, , . iconv ICU.)

0

Windows:

//#include <windows.h>
u16string utext = u"";
char text[0x100];
WideCharToMultiByte(CP_UTF8,NULL,(const wchar_t*)(utext.c_str()),-1,text,-1,NULL,NULL);
cout << text;

std:: wstring_convert, MinGW 4.9.2.

0

All Articles