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?
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?
WideCharToMultiByte() を使用します – CP_UTF8 を最初のパラメーターとして渡します。
BSTR は null ポインターになる可能性があり、空の文字列に対応することに注意してください。これは特殊なケースとして扱ってください。
この種の非常に便利な MSDN リファレンス: http://msdn.microsoft.com/en-us/library/ms235631(VS.80).aspx
よくわかりませんが、char* は Unicode のものを失うので、wchar_t* に行く必要があると思います。
// convert_from_bstr_t.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
using namespace std;
using namespace System;
int main()
{
_bstr_t orig("Hello, World!");
wcout << orig << " (_bstr_t)" << endl;
// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
strcpy_s(nstring, (char *)orig);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
wchar_t wcstring[newsize];
wcscpy_s(wcstring, (wchar_t *)orig);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a CComBSTR
CComBSTR ccombstr((char *)orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring((char *)orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
string basicstring((char *)orig);
basicstring += " (basic_string)";
cout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String((char *)orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}