Paavo Helde
11/19/2008 9:08:00 PM
"Jung, William" <aopiyy001@yahoo.com> kirjutas:
> I have a function that convert a string to binary, where
> - string is the string needs to convert to binary.
> - binary is the BYTE array to hold the converted data
>
> StringtoBinary( LPCSTR string, BYTE *binary)
>
> Since BYTE is a pointer to BYTE,
No, BYTE is BYTE, "binary" is a pointer to a BYTE, and most probably to
the first one in the array of BYTE-s the function expects.
> do I need to use new operator to
> allocate space / storage (dynmaic allocation)?
No, you do not. You have to pass a pointer to a buffer large enough. How
the buffer is allocated should not be a concern.
StringtoBinary() has to document somehow large buffer it expects. Then
you prepare this beforehand:
std::vector<BYTE> buffer(needed_size_in_BYTEs);
StringtoBinary(mystring, &buffer[0]);
In case of conversions it might often happen that the function does not
fill the whole buffer and reports back instead how much it actually used.
Then you can resize the buffer to reflect this:
buffer.resize(actually_used_size_in_BYTEs);
If you have control over StringtoBinary() implementation, you can make it
more like C++. I mean, in C++ the caller should not deal with such
details as buffer allocation and resizing.
hth
Paavo