#include <windows.h> #include <string> /// @brief ascii和unicode之间的编码转换 size_t ascii_to_unicode(const char* src, wchar_t* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = strlen(src); return MultiByteToWideChar(CP_ACP, 0, src, static_cast<int>(src_len), dest, static_cast<int>(dest_len)); } size_t unicode_to_ascii(const wchar_t* src, char* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = wcslen(src); return WideCharToMultiByte(CP_ACP, 0, src, static_cast<int>(src_len), dest, static_cast<int>(dest_len), NULL, NULL); } /// @brief utf8和unicode之间的编码转换 size_t utf8_to_unicode(const unsigned char* src, wchar_t* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = strlen((const char*)src); return MultiByteToWideChar(CP_UTF8, 0, (const char*)src, static_cast<int>(src_len), dest, static_cast<int>(dest_len)); } size_t unicode_to_utf8(const wchar_t* src, unsigned char* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = wcslen(src); return WideCharToMultiByte(CP_UTF8, 0, src, static_cast<int>(src_len), (char*)dest, static_cast<int>(dest_len), NULL, NULL); } /// @brief ascii和utf8之间的编码转换 size_t ascii_to_utf8(const char* src, unsigned char* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = strlen(src); std::wstring unicode(src_len, 0); unicode.resize( ascii_to_unicode(src, const_cast<wchar_t*>(unicode.data()), unicode.size(), src_len)); return unicode_to_utf8(unicode.da ta(), dest, dest_len, unicode.size()); } size_t utf8_to_ascii(const unsigned char* src, char* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = strlen((const char*)src); std::wstring unicode(src_len * 2, 0); unicode.resize( utf8_to_unicode(src, const_cast<wchar_t*>(unicode.da ta()), unicode.size(), src_len)); return unicode_to_ascii(unicode.da ta(), dest, dest_len, unicode.size()); } /// @brief 普通字符串转宽字符串 std::wstring& to_wstring(const char* str, size_t len, std::wstring& ret) { if(len == 0) len = strlen(str); ret.resize(len); ret.resize(ascii_to_unicode(str, const_cast<wchar_t*>(ret.da ta()), ret.size(), len)); return ret; } std::wstring to_wstring(const char* str, size_t len = 0) { std::wstring ret; return to_wstring(str, len, ret); } std::wstring to_wstring(const std::string& str) { std::wstring ret; return to_wstring(str.da ta(), str.size(), ret); } std::wstring& to_wstring(const std::string& str, std::wstring& ret) { return to_wstring(str.da ta(), str.size(), ret); } /// @brief 宽字符串转普通字符串 std::string& to_string(const wchar_t* str, size_t len, std::string& ret) { if(len == 0) len = wcslen(str); ret.resize(len * 2); ret.resize(unicode_to_ascii(str, const_cast<char*>(ret.da ta()), ret.size(), len)); return ret; } std::string to_string(const wchar_t* str, size_t len) { std::string ret; return to_string(str, len, ret); } std::string to_string(const std::wstring& str) { std::string ret; return to_string(str.da ta(), str.size(), ret); } std::string& to_string(const std::wstring& str, std::string& ret) { return to_string(str.da ta(), str.size(), ret); }
}