how do i convert CComBstr to char;
char c;
CComBstr code;
c = *code;
is the above conversion correct or is there any other method ?
please help.its urgent....
Printable View
how do i convert CComBstr to char;
char c;
CComBstr code;
c = *code;
is the above conversion correct or is there any other method ?
please help.its urgent....
If you know that the first character is a simple ASCII value (in the range of 0x00-0x1F), then you could simply use:
ggCode:char c = static_cast<char>(*code.m_str);
See the CComBSTR::operator = in msdn.
Code:CComBstr code(_T("Hello World!"));
// Get the string as ANSI or UNICODE
LPCSTR sz = code;
// Retrieve the ANSI or UNICODE character
TCHAR c = sz[0]; // 'H'
Doesn't compile.
"CComBSTR" - and it doesn't have an "operator LPCSTR".
ggCode:#include <atlbase.h>
#include <iostream>
using namespace std;
int main()
{
CComBSTR code(_T("Hello World!"));
char c = static_cast<char>(*code.m_str);
cout << c << endl;
return 0;
}//main
No cast, as BSTR is WCHAR[] inside. Therefore, conversion is needed from WCHAR[] to CHAR[]:
Code:#include <stdio.h>
#include <stdlib.h>
#include <atlbase.h>
int main()
{
CComBSTR bstr(L"Hello World!");
CHAR mbs[20] = {0};
wcstombs(mbs, bstr, sizeof(mbs));
printf("%s\n", mbs);
return 0;
}
Looks like the msdn link I gave was incorrect.
No biggie - just use the string conversion macro.
The above works in ANSI and UNICODE.Code:CComBSTR code( _T("Hello World!") );
USES_CONVERSION;
LPTSTR sz = OLE2T( code );
TCHAR c = sz[0];
When working in Windows code, any code that uses 'char' makes me cringe ('cuz I now it's going to be a pain if I ever need to port the code to UNICODE).
>> ** how do i convert CComBstr to char **
I don't have a crystal ball either - so I only answered the quested asked by the OP (under the condition that it's an ASCII value less than 0x1F).
They didn't ask how to convert to a TCHAR, TCHAR[], or CHAR[] - but now this thread contains solutions to all of those :)
>> Looks like the msdn link I gave was incorrect.
No, its correct.
gg
It's there - and it works.
ggCode:#include <atlbase.h>
#include <iostream>
using namespace std;
int main()
{
CComBSTR code;
code = "Hello World";
char c = static_cast<char>(*code.m_str);
cout << c << endl;
return 0;
}//main
I didn't really think you needed a C++ lesson...
First, LPCSTR is such a thing - it's a pointer to a const CHAR string.
Second, "operator=(LPCSTR)" is invoked when there is an LPCSTR on the right-hand-side of the object. Which is why my example included {code = "Hello World"}. It compiles without error. "It" obviously exists - as the documentation indicates.
>> LPCSTR sz = code;
Your code here does not compile because there is no "operator LPCSTR()" - which I mentioned in post #4. This is also refered to as a "cast operator". operator LPCSTR() would be invoked here, if it existed.
gg
Thank's a lot for the replies.