繁体   English   中英

将 SQLWCHAR * 转换为 char*

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM