I have a _bstr_t
string which contains Japanese text. I want to convert this string to a UTF-8 string which is defined as a char *
.
Can I convert the _bstr_t
string to char *
(UTF-8) string without losing the Japanese characters?
Use WideCharToMultiByte() – pass CP_UTF8 as the first parameter.
Beware that BSTR can be a null pointer and that corresponds to an empty string – treat this as a special case.
Here is some code that should do the conversion.
void PrintUtf8(const TCHAR* value) {
if (value == nullptr) {
printf("");
return;
}
int n = WideCharToMultiByte(CP_UTF8, 0, value, -1, nullptr, 0, nullptr, nullptr);
if (n <= 0) {
printf("");
return;
}
char* buffer = new char[n];
WideCharToMultiByte(CP_UTF8, 0, value, -1, buffer, n, nullptr, nullptr);
printf("%s", buffer);
delete(buffer);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With