[英]Converting SQLWCHAR * to char*
第一次在unicode和widechars地區工作,
我正在嘗試將WCHAR*
轉換為char*
。 (WCHAR 類型定義為 SQLWCHAR,最終為unsigned short
)
而且我需要支持所有平台(windows、mac、linux) 我作為輸入的是一個 WCHAR* 及其length 。 我想,如果我將輸入轉換為wstring
,我將有機會將其 strcpy/strdup 轉換為 output 變量。
但看起來我沒有正確構造我的 wstring 因為 wprintf 沒有打印它的值。
任何提示我錯過了什么?
#include <iostream>
#include <codecvt>
#include <locale>
SQLRETURN SQL_API SQLExecDirectW(SQLHSTMT phstmt, SQLWCHAR* pwCmd, SQLINTEGER len)
{
char *output;
wchar_to_utf8(pwCmd, len, output);
// further processing
SQLRETURN rc;
return rc;
}
int wchar_to_utf8(WCHAR *wStr, int size, char *output)
{
std::wstring ws((const wchar_t*)wStr, size - 1);
wprintf(L"wstring:%s\n", ws);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
std::string t = conv.to_bytes(ws);
/*allocate output or use strdup*/
strncpy(output, t.c_str(), size); // todo:take care of the last null char
return strlen(output);
}
wprintf
格式字符串中的"%s"
需要一個wchar_t*
參數,因此
wprintf(L"wstring%s\n", ws);
應該
wprintf(L"wstring%s\n", ws.c_str());
將 wchar* 轉換為 char*
const std::wstring wStr = /*your WCHAR* */;
const char* str = std::filesystem::path(wStr).string().c_str();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.