Hi All,

The variable str was defined as CStringA type, and it contains 57 chars with
embedded NULL chars copied from some memory buffer, now I want to convert it
into a BSTR string using the following statements:

1) int nLen = str.GetLength();
2) int nChars = MultiByteToWideChar(CP_ACP, NULL, str, nLen, NULL, NULL);
3) bstr = SysAllocStringLen(NULL, nChars);
4) MultiByteToWideChar(CP_ACP, NULL, str, nLen, bstr, nChars);

at line#1 I got nLen 57, but at line#2, I got nChars 50, why nChars should
not be 57? Am I doing something wrong?

PS: The str is "50 4b 01 02 14 00 14 00 02 00 08 00 60 8b 16 33 00 00 60 8b
ff ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 ff ff 00 00 00 00 00 00 00 00
bb b0 fb c2 ee 9d 8f 0e de 04 00 00"

TIA

Re: How to convert CStringA which contains embedded NULL chars into a by Ulrich

Ulrich
Mon Aug 22 07:57:44 CDT 2005

Kaibin wrote:
> The variable str was defined as CStringA type, and it contains 57 chars
> with embedded NULL chars copied from some memory buffer,
[...]
> PS: The str is "50 4b 01 02 14 00 14 00 02 00 08 00 60 8b 16 33 00 00 60
> 8b ff ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 ff ff 00 00 00 00 00 00
> 00 00 bb b0 fb c2 ee 9d 8f 0e de 04 00 00"

I daresay you are using the wrong tool. A string is a sequence of characters
terminated by a trailing NUL char, a.k.a. text. The data you have seems to
be some kind of raw memory format (also called "binary"), so CStringA is
simply not the right container to hold and handle it.

Uli


Re: How to convert CStringA which contains embedded NULL chars int by Kaibin

Kaibin
Mon Aug 22 08:46:09 CDT 2005

You are right, Uli. It's a BINARY stream results from DynaZip
MemToMemCompressProc callback function. Which data type you recommended to
handles it? Also, I have to convert the BINARY into BSTR.

Thank you!

"Ulrich Eckhardt" wrote:

> Kaibin wrote:
> > The variable str was defined as CStringA type, and it contains 57 chars
> > with embedded NULL chars copied from some memory buffer,
> [...]
> > PS: The str is "50 4b 01 02 14 00 14 00 02 00 08 00 60 8b 16 33 00 00 60
> > 8b ff ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 ff ff 00 00 00 00 00 00
> > 00 00 bb b0 fb c2 ee 9d 8f 0e de 04 00 00"
>
> I daresay you are using the wrong tool. A string is a sequence of characters
> terminated by a trailing NUL char, a.k.a. text. The data you have seems to
> be some kind of raw memory format (also called "binary"), so CStringA is
> simply not the right container to hold and handle it.
>
> Uli
>
>

Re: How to convert CStringA which contains embedded NULL chars int by adebaene

adebaene
Mon Aug 22 08:52:55 CDT 2005


Kaibin a =E9crit :

> You are right, Uli. It's a BINARY stream results from DynaZip
> MemToMemCompressProc callback function. Which data type you recommended to
> handles it?
A std::vector<BYTE> should be fine for most of the needs.

> Also, I have to convert the BINARY into BSTR.
What do you call BINARY, and how do you want to represent it in string
(eg, do you want to display a string made of '0' and '1's? an
hexadecimal or decimal representation of the data? consider that the
binary data is ASCII? Something else?

Arnaud
MVP - VC


Re: How to convert CStringA which contains embedded NULL chars int by Igor

Igor
Mon Aug 22 08:56:14 CDT 2005

Kaibin <Kaibin@community.nospam> wrote:
> You are right, Uli. It's a BINARY stream results from DynaZip
> MemToMemCompressProc callback function. Which data type you
> recommended to handles it? Also, I have to convert the BINARY into
> BSTR.

BSTR is really just as unsuitable for carrying binary data as CString.
If you need to communicate with an automation COM object, consider a
safearray of bytes.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: How to convert CStringA which contains embedded NULL chars int by Alex

Alex
Mon Aug 22 10:27:06 CDT 2005

Igor Tandetnik wrote:
> BSTR is really just as unsuitable for carrying binary
> data as CString. If you need to communicate with an
> automation COM object, consider a safearray of bytes.

Actually, BSTR can be used as cheap binary container
provided that BSTR won't undergo any ANSI <-> Unicode
conversions. This is stated explicitly here:

"SysAllocStringByteLen"
http://msdn.microsoft.com/library/en-us/automat/htm/chap7_3ub2.asp

However, I agree with you that robust COM binary container
should use safearray.