|
-
August 24th, 2011, 01:11 AM
#1
convert CComBstr to char
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....
-
August 24th, 2011, 11:30 AM
#2
Re: convert CComBstr to char
If you know that the first character is a simple ASCII value (in the range of 0x00-0x1F), then you could simply use:
Code:
char c = static_cast<char>(*code.m_str);
gg
-
August 24th, 2011, 12:28 PM
#3
Re: convert CComBstr to char
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'
-
August 24th, 2011, 12:41 PM
#4
Re: convert CComBstr to char
Doesn't compile.
"CComBSTR" - and it doesn't have an "operator LPCSTR".
Code:
#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
gg
-
August 24th, 2011, 12:53 PM
#5
Re: convert CComBstr to char
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;
}
Best regards,
Igor
-
August 24th, 2011, 12:56 PM
#6
Re: convert CComBstr to char
 Originally Posted by Codeplug
Doesn't compile.
"CComB STR" - and it doesn't have an "operator LPCSTR".
Code:
#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
gg
Looks like the msdn link I gave was incorrect.
No biggie - just use the string conversion macro.
Code:
CComBSTR code( _T("Hello World!") );
USES_CONVERSION;
LPTSTR sz = OLE2T( code );
TCHAR c = sz[0];
The above works in ANSI and UNICODE.
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).
-
August 24th, 2011, 12:59 PM
#7
Re: convert CComBstr to char
 Originally Posted by Codeplug
If you know that the first character is a simple ASCII value (in the range of 0x00-0x1F), then you could simply use:
Code:
char c = static_cast<char>(*code.m_str);
gg
I don't have my crystal ball here with me (I am at work), but I am pretty sure OP doesn't just want one character from that BSTR.
The issue is that BSTR is a pointer to WCHAR, so a conversion (like W2A) is needed.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 24th, 2011, 01:15 PM
#8
Re: convert CComBstr to char
>> ** 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
-
August 24th, 2011, 01:21 PM
#9
Re: convert CComBstr to char
 Originally Posted by Codeplug
>> Looks like the msdn link I gave was incorrect.
No, its correct.
While the link is correct, the content within the link is incorrect.
The link shows the following operator = overload...
Code:
CComBSTR& operator =(
LPCSTR pSrc
);
It doesn't exist and there is no such thing as LPCSTR.
-
August 24th, 2011, 01:39 PM
#10
Re: convert CComBstr to char
It's there - and it works.
Code:
#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
gg
-
August 24th, 2011, 01:47 PM
#11
Re: convert CComBstr to char
 Originally Posted by Codeplug
It's there - and it works.
If you are referring to my response, it isn't there.
Notice the difference between your code
and mine
My code uses the LPOLESTR = operator, yours accesses the underlying BSTR pointer.
You're getting lucky with your code.
Last edited by Arjay; August 24th, 2011 at 01:51 PM.
-
August 24th, 2011, 02:09 PM
#12
Re: convert CComBstr to char
 Originally Posted by Arjay
While the link is correct, the content within the link is incorrect.
The link shows the following operator = overload...
Code:
CComBSTR& operator =(
LPCSTR pSrc
);
It doesn't exist and there is no such thing as LPCSTR.
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
-
August 24th, 2011, 02:24 PM
#13
Re: convert CComBstr to char
 Originally Posted by Codeplug
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
Thanks. Everyone once in a while things fall through the cracks. Still don't like the char though.
-
August 25th, 2011, 02:41 AM
#14
Re: convert CComBstr to char
Thank's a lot for the replies.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|