How to vopy data pointed by a "Void* " pointer to "CString" object
-
Hi, I have to copy the data pointed by "void*" to a CString object. Which is the best way? code: void* pVoid = 0; CString csData; pVoid = new BYTE(100); // Do some thing // ??? (Here I have to copy data from void* to CString.) delete[] pVoid; AfxMessageBox( csData ); Thnx in advance.
-
Hi, I have to copy the data pointed by "void*" to a CString object. Which is the best way? code: void* pVoid = 0; CString csData; pVoid = new BYTE(100); // Do some thing // ??? (Here I have to copy data from void* to CString.) delete[] pVoid; AfxMessageBox( csData ); Thnx in advance.
Use a typecast i.e. csData = (char*)pVoid; You should probably validate the data in pVoid first, not sure how CString would respond if the data wasn't NULL terminated.
-
Hi, I have to copy the data pointed by "void*" to a CString object. Which is the best way? code: void* pVoid = 0; CString csData; pVoid = new BYTE(100); // Do some thing // ??? (Here I have to copy data from void* to CString.) delete[] pVoid; AfxMessageBox( csData ); Thnx in advance.
void* pVoid = 0; CString csData; pVoid = new BYTE(100); I would do the following: memcpy(&csData, pVoid, 100); //I would use a #define for the 100 tho because it make sit easier to understand Now this has copied the data from one object to another WITHOUT using a reference.
-
Hi, I have to copy the data pointed by "void*" to a CString object. Which is the best way? code: void* pVoid = 0; CString csData; pVoid = new BYTE(100); // Do some thing // ??? (Here I have to copy data from void* to CString.) delete[] pVoid; AfxMessageBox( csData ); Thnx in advance.
DO NOT USE THE IMMEDIATE ABOVE SUGGESTION; it will result in a buffer overrun and is VERY poor coding practice. Two general methods: 1) Cast the void pointer to a char, tchar or wchar_t pointer. 2) Call CString::GetBuffer() with the length of buffer you require. Copy the data in and then call CString::ReleaseBuffer(). You will want to to the latter if your buffer contains multiple zero terminated strings since method one will terminate the copy at the first null.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke