如果只有字节大小已知,是否将UTF-16缓冲区转换为CString



在https://www.sqlite.org/c3ref/column_blob.html,我只能得到一个指向UTF-16文本缓冲区的指针,以及缓冲区中的字节数。

现在我需要将这样一个缓冲区转换为CString对象。如何做到这一点?CString似乎只有以下构造函数:

CString( LPCTSTR lpch, int nLength );  // requires LPCTSTR and number of chars, not bytes
CString( LPCWSTR lpsz );               // requires null-terminiated Unicode buffer

两者似乎都不适合我的情况。

CString( LPCTSTR lpch, int nLength )将完成此项工作。它只需要LPCTSTR强制转换,在本例中为LPCWSTRnLength应为大小除以2以说明wchar_t

如果您的程序不是Unicode,请使用CStringW

示例:

//create a buffer (buf) and copy a wide string in to it
const wchar_t *source = L"ABC";
int len = wcslen(source);
int bytesize = len * 2;
BYTE *buf = new BYTE[bytesize + 2]; //optional +2 for null terminator
memcpy(buf, source, bytesize);
//copy buf into destination CString
CString destination((LPCWSTR)buf, bytesize / 2);
delete[]buf;
MessageBoxW(0, destination, 0, 0);

bufbytesize来自数据库,所以只需键入:

CString destination((LPCWSTR)buf, bytesize / 2);    
//or
CStringW destination((LPCWSTR)buf, bytesize / 2);

最新更新