Link to home
Start Free TrialLog in
Avatar of ol muser
ol muserFlag for United States of America

asked on

data conversion c++ string

I am getting data from a hardware API in the format below:

typedef struct _CRYPTOAPI_BLOB {
                                          DWORD   cbData;
      __field_bcount(cbData)  BYTE    *pbData;
} DATA_BLOB, *PDATA_BLOB;

The data returned as I can see in the debug window has a valid non-zerovalue for
cbData. But pbData[0] invariably has '\0'. My first question is, is that valid as
I am getting binary data, possibly the first character is null and yet I have valid data
in the rest of the memory locations?

My second question is, assuming  I have valid data, about converting this to a
string so I can pass this data to a socket. I plan to use a std::vector<char>.
but for now to use existing function that accepts a string argument what is a safe way of
converting?

method 1:

      PDATA_BLOB xxx; //returned with values
      std::string rawdata((char*)xxx->pbData, xxx->cbData);

currently I use this method and invaribaly the string rawdata is null ("").
not sure if it is bcoz I receive null data or if the conversion fails, and hence my
first question

method 2:
for (i = 0; i < xxx->cbData; i++) {
  string[i] = xxx->pbData[i];
}

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ol muser

ASKER

for some reason string didn't do it, vector<char> does the trick!
i think the string problem is only a visual issue. whenever you look to a string with binary zeros with debugger you won't see the data. also the c_str member function would return an empty value because of the leading zero.

but vector<char> is more than an equivalent choice.

Sara