I want to convert a char
array
, e.g. char myArray[size]
to a _bstr_t
string. I tried this, but it doesn't work:
_bstr_t test;
for (int i = 0; i < myArrayLength; i++) {
test = test + (_bstr_t) myArray[i];
}
I want to convert a char
array
, e.g. char myArray[size]
to a _bstr_t
string. I tried this, but it doesn't work:
_bstr_t test;
for (int i = 0; i < myArrayLength; i++) {
test = test + (_bstr_t) myArray[i];
}
The _bstr_t
class provides a conversion c'tor (_bstr_t::_bstr_t) that takes a const char*
. It performs character conversion from ANSI code page encoding (using the thread's current locale) to UTF-16, and constructs a _bstr_t
object:
_bstr_t bstr = _bstr_t(myArray);
Note, that there is also a binary _bstr_t::operator+(), that takes a const char*
as its left-hand side. The following is also allowed:
_bstr_t test;
...
_bstr_t bstr = myArray + test;
char
array uses an encoding other than ASCII or ANSI using the current code page, you will have to manually convert from the source encoding to UTF-16 using MultiByteToWideChar, and construct a _bstr_t
from the wchar_t
array.