0
votes

I am new to vc++. How to convert UCHAR * value to CStringand CString to UCHAR *

CString str;
UCHAR * pBuffer;
......Memmory allocation..
str.format(_T("%d"),pBuffer);

But its not working. Second data may be string or int so how to do conversion in proper way.

1
In above link answer is accepted. But questioner is not satisfied with answer. - abhi312
What EXACTLY does the UCHAR* represent? - Remy Lebeau

1 Answers

1
votes

Second data may be string or int so how to do conversion in proper way.

You have to interpret the UCHAR* data as a proper data type when formatting it, eg:

// if pBuffer contains 'int' data...
str.format(_T("%d"), *(int*)pBuffer);

// if pBuffer contains 8bit 'char' data w/ a null terminator...
str.format(_T("%hs"), (char*)pBuffer);

Or:

// if pBuffer contains 8bit 'char' data w/o a null terminator...
str.format(_T("%*hs"), iBufferLen, (char*)pBuffer);

And so on...