How to correctly convert unsigned char to CString and once again, reversely convert to the result of CString to unsigned char?

冷暖自知 提交于 2019-12-06 03:39:52

Conversion to CString is pretty easy, just pass the unsigned char* to the c'tor. Conversion from CString to unsigned char* is a little more work, see below.

unsigned char orig[] = "hello world";
std::cout << orig << " (unsigned char *)" << std::endl;

// Convert to a CString
CString cstring(orig);
std::cout << cstring << " (CString)" << std::endl;

// Convert to a unsigned char*
const size_t newsize = (cstring.GetLength() + 1);
unsigned char* nstring = new unsigned char[newsize];
strcpy_s((char*)nstring, newsize, cstring);
std::cout << nstring << " (unsigned char*)" << std::endl;

Take a look at documentation, you can construct CString from a pointer to char buffer MSDN. All supported operations for CString class, you may use GetBuffer, function to get a pointer to internal buffer of the CString, call ReleaseBuffer to take ownership of this buffer.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!