StrConv.h
View Code
#pragma once #include <string> #ifdef _UNICODE #ifndef tchar #define tchar wchar_t #endif #ifndef tstring #define tstring std::wstring #endif #else #ifndef tchar #define tchar char #endif #ifndef tstring #define tstring std::string #endif #endif // _UNICODE ////////////////////////////////////////////////////////////////////////// // ANSI <-> UTF8 <-> UTF16 ////////////////////////////////////////////////////////////////////////// std::wstring AnsiToUtf16(const std::string& str); std::string Utf16toUtf8(const std::wstring& str); std::string AnsiToUtf8(const std::string& str); std::string TStrToUtf8(const tstring& str); std::wstring Utf8ToUtf16(const std::string& str); std::string Utf16ToAnsi(const std::wstring& str); std::string Utf8ToAnsi(const std::string& str); tstring Utf8ToTStr(const std::string& str); #ifdef _UNICODE #define TStrToAnsi(str) Utf16ToAnsi(str) #define TStrToUtf16(str) (str) #define AnsiToTStr(str) AnsiToUtf16(str) #define Utf16ToTStr(str) (str) #else #define TStrToAnsi(str) (str) #define TStrToUtf16(str) AnsiToUtf16(str) #define AnsiToTStr(str) (str) #define Utf16ToTStr(str) Utf16ToAnsi(str) #endif
StrConv.cpp
View Code
#include "StrConv.h" #include <windows.h> std::wstring AnsiToUtf16(const std::string& str) { std::wstring res; int bufferSize = MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.size() + 1, NULL, 0); if (bufferSize) { wchar_t* buffer = new wchar_t[bufferSize]; if (MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.size() + 1, buffer, bufferSize)) res = buffer; delete[] buffer; buffer = NULL; } return res; } std::string Utf16toUtf8(const std::wstring& str) { std::string res; int bufferSize = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.size() + 1, NULL, 0, NULL, NULL); if (bufferSize) { char* buffer = new char[bufferSize]; if (WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.size() + 1, buffer, bufferSize, NULL, NULL)) res = buffer; delete[] buffer; buffer = NULL; } return res; } std::string AnsiToUtf8(const std::string& str) { return Utf16toUtf8(AnsiToUtf16(str)); } std::string TStrToUtf8(const tstring& str) { #ifdef _UNICODE return Utf16toUtf8(str); #else return AnsiToUtf8(str); #endif } std::wstring Utf8ToUtf16(const std::string& str) { std::wstring res; int bufferSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size() + 1, NULL, 0); if (bufferSize) { wchar_t* buffer = new wchar_t[bufferSize]; if (MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size() + 1, buffer, bufferSize)) res = buffer; delete[] buffer; buffer = NULL; } return res; } std::string Utf16ToAnsi(const std::wstring& str) { std::string res; int bufferSize = WideCharToMultiByte(CP_ACP, 0, str.c_str(), (int)str.size() + 1, NULL, 0, NULL, NULL); if (bufferSize) { char* buffer = new char[bufferSize]; if (WideCharToMultiByte(CP_ACP, 0, str.c_str(), (int)str.size() + 1, buffer, bufferSize, NULL, NULL)) res = buffer; delete[] buffer; buffer = NULL; } return res; } std::string Utf8ToAnsi(const std::string& str) { return Utf16ToAnsi(Utf8ToUtf16(str)); } tstring Utf8ToTStr(const std::string& str) { #ifdef _UNICODE return Utf8ToUtf16(str); #else return Utf8ToAnsi(str); #endif }