简体   繁体   中英

Convert std::wstring to std::string

I have the following line of code

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

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

I'm not sure this would work with Unicode characters but it works for me:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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