#include <cstring>
#include<iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
#include <Windows.h>
WCHAR* char2wchar(char* data, size_t size) {
unsigned int number = MultiByteToWideChar(CP_ACP, 0, data, size, NULL, 0);
WCHAR* buffer = new WCHAR[number + 1];
memset(buffer, 0, (number + 1) * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, data, size, buffer, number);
return buffer;
}
char* wchar2char(WCHAR* data, size_t size) {
unsigned int number = WideCharToMultiByte(CP_ACP, 0, data, size, NULL, 0, NULL, NULL);
char* buffer = new char[number + 1];
memset(buffer, 0, (number + 1) * sizeof(char));
WideCharToMultiByte(CP_ACP, 0, data, size, buffer, number, NULL, NULL);
return buffer;
}
//将string转换成wstring
std::wstring string2wstring(std::string str) {
unsigned int number = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
WCHAR* buffer = new WCHAR[number + 1];
memset(buffer, 0, (number + 1) * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, number);
std::wstring result(buffer);
delete[] buffer;
return result;
}
//将wstring转换成string
std::string wstring2string(std::wstring wstr) {
unsigned int number = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
// need space for \0
char* buffer = new char[number + 1];
memset(buffer, 0, (number + 1) * sizeof(char));
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, number, NULL, NULL);
std::string result(buffer);
std::cout << result <<" "<< number << std::endl;
delete[] buffer;
return result;
}
int main() {
std::wstring a = L"D:/c++/vs_data/create_ce_dll/x64/Release/啊啊还是都爱说大话/create_ce_dll.dll";
std::cout << a.size() << std::endl;
std::string ans1 = wstring2string(a);
std::wstring b = string2wstring(ans1);
char* ans2 = wchar2char((WCHAR *)a.c_str(), a.size());
printf("%s\n", ans1.c_str());
printf("%s\n", ans2);
WCHAR * mywchar = char2wchar(ans2, strlen(ans2));
char *ans3 = wchar2char(mywchar, a.size());
int flag = 0;
flag = strcmp(ans2, ans3);
//int flag = wcscmp(mywchar, a.c_str());
printf("%d\n", flag);
free(ans2);
free(mywchar);
free(ans3);
return 0;
}