简体   繁体   English

将 std::wstring 转换为 std::string

[英]Convert std::wstring to std::string

I have the following line of code我有以下代码行

const wchar_t* test = L"2017---扫描"; const wchar_t* test = L"2017---扫描";

I want to convert the above test variable to std::string as the other function takes only std::string type, something like我想将上述测试变量转换为 std::string 因为另一个 function 只需要 std::string 类型,类似于

const char* testChar = "2017---扫描"; const char* testChar = "2017---扫描";

I'm not sure this would work with Unicode characters but it works for me:我不确定这是否适用于 Unicode 字符,但它适用于我:

header header

#include <string>
#include "atlbase.h"

main主要的

const wchar_t* test = L"2017--aa";
size_t origsize = wcslen(test) + 1;
size_t convertedChars = 0;
const size_t newsize = origsize * 2;
char* testChar = new char[newsize];
wcstombs_s(&convertedChars, testChar, newsize, test, _TRUNCATE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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