string - Convert a UTF-16 buffer into CString if only byte size is Known?
问题描述
When using sqlite3_column_text16 and sqlite3_column_bytes16 in https://www.sqlite.org/c3ref/column_blob.html , I can only get a pointer to the UTF-16 text buffer, as well as the number of bytes in the buffer.
Now I need to convert such a buffer into a CString object. How to do so? It seems that CString only has the following constructor:
CString( LPCTSTR lpch, int nLength ); // requires LPCTSTR and number of chars, not bytes
CString( LPCWSTR lpsz ); // requires null-terminiated Unicode buffer
Both seems not appropriate for my case.
解决方案
CString( LPCTSTR lpch, int nLength )
将完成这项工作。它只需要一个LPCTSTR
演员,或者LPCWSTR
在这种情况下。nLength
应该是大小除以 2 来说明wchar_t
CStringW
如果您的程序不是 Unicode,请使用。
例子:
//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);
buf
并且bytesize
来自数据库,所以只需输入:
CString destination((LPCWSTR)buf, bytesize / 2);
//or
CStringW destination((LPCWSTR)buf, bytesize / 2);
推荐阅读
- c# - HTTP polling VS Websocket VS Webhooks 之间是否存在显着的资源使用差异?
- wordpress - 如何使用来自 get_the_content() 的原始数据作为变量
- python - 如何测试 PyVista 是否成功绘制了图形?
- machine-learning - 我可以使用 MCA 的结果使用 K-means、DBScan 或 GMM 进行聚类吗?
- reactjs - React-widgets DateTimePicker 无法正常工作,给出无法分配给类型“只读”的错误
' - python - 覆盖 object.__new__ 时的不一致
- python - 1.0 作为布尔数据类型导致 pd.read_csv() 中的 ValueError
- python - 用于在 Raspberry pi 上读取传感器数据的库
- c# - 如何在 C# 中获取引用的 dll 的完整路径
- kubernetes - Kubernetes 中的 Prometheus HA (AKS)