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


LVL 1
ol muserTechnology GeneralistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> My first question is, is that valid as I am getting binary data

A 0 byte is very likely a valid value indeed, since you're not dealing with a string, but rather with binary data.


>> My second question is, assuming  I have valid data, about converting this to a string so I can pass this data to a socket.

That depends on how you initialize the string, and on how the socket uses the string.

But it's very likely that it will not be handled correctly, and will be stopping at the first 0 byte (which happens to be the very first byte in this specific case).

You should probably pass an array (or vector if you wish) of bytes (unsigned char eg.) to the socket, together with the length of the array (ie. the amount of bytes).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
if you are aware that a string with a binary zero as first char will give problems when evaluated with the debugger or when printed as a string, you can use it as a buffer without problems. the

std::string rawdata((char*)xxx->pbData, xxx->cbData);

will store the data correctly and you will have rawdata.size() == xxx.cbData.

i have done the same in various projects and never had problems (beside of the visuality) with that.

you would get access to the internal char array by &rawdata[0] (though that is not guaranteed by the standard as it is with std::vector<char>).

Sara
0
ol muserTechnology GeneralistAuthor Commented:
for some reason string didn't do it, vector<char> does the trick!
0
sarabandeCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.