因为网络编程的需要,参考了各种地方,自己造的一个C++11的轮子
如有错误请指出
#ifndef TRANSCODING_HPP
#define TRANSCODING_HPP
#include <Windows.h>
#include <iostream>
#include <memory>
const std::wstring StringToWstring(const std::string& str)
{
if(str.empty()) throw std::runtime_error("string is empty");
int size = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), nullptr, 0);
if (size == 0)
{
throw std::runtime_error("String conversion error");
}
auto p_wstr(std::make_unique<wchar_t[]>(size + 1));
if (MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), p_wstr.get(), size) == 0)
{
throw std::runtime_error("String conversion error");
}
return std::wstring(p_wstr.get());
}
const std::string WstringToString(const std::wstring& str)
{
if(str.empty()) throw std::runtime_error("wstring is empty");
int size = WideCharToMultiByte(CP_ACP