C++11 的编码转换程序:
#ifndef UNISTRING_HPP #define UNISTRING_HPP #include <algorithm> #include <codecvt> #include <cstdio> #include <cstdarg> #include <iostream> #include <string> #include <sstream> // locale = "chs" | "zh-cn" //const std::string language = "chs"; const std::string language = "zh-cn"; inline void init_locale() { std::locale::global(std::locale(language)); } // 转换成小写 template<typename T> inline std::basic_string<T> to_lower(const std::basic_string<T>& str) { std::basic_string<T> product(str); for (auto& ch : product) { ch = std::tolower(ch, std::locale()); } return product; } // 转换成大写 template<typename T> inline std::basic_string<T> to_upper(std::basic_string<T> str) { std::basic_string<T> product(str); for (auto& ch : product) { ch = std::toupper(ch, std::locale()); } return product; } // unicode 字符串转换成 ansi 字符串 inline std::string to_ansi(const wchar_t* str, size_t length) { typedef std::codecvt_byname<wchar_t, char, std::mbstate_t> arg_t; static std::wstring_convert<arg_t> convertor(new arg_t(language)); return convertor.to_bytes(str, str + length); } inline std::string to_ansi(const std::wstring& str) { return to_ansi(str.c_str(), str.length()); } // ansi 字符串转换成 unicode 字符串 inline std::wstring to_unicode(const char* str, size_t size) { typedef std::codecvt_byname<wchar_t, char, std::mbstate_t> arg_t; static std::wstring_convert<arg_t> convertor(new arg_t(language)); return convertor.from_bytes(str, str + size); } inline std::wstring to_unicode(const std::string& str) { return to_unicode(str.c_str(), str.length()); } // utf8 字符串转 unicode 字符串 inline std::wstring utf8_to_unicode(const std::string& str) { std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; return convertor.from_bytes(str); } // unicode 字符串转 utf8 字符串 inline std::string to_utf8(const std::wstring& str) { std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; return convertor.to_bytes(str); } // ansi 字符串转 utf8 字符串 inline std::string to_utf8(const std::string& str) { return to_utf8(to_unicode(str)); } // 字符串转其他类型 template<typename T, typename _char_t> inline T to_numeric(const std::basic_string<_char_t>& str) { std::basic_stringstream<_char_t> stm(str); T n; stm >> n; return n; } // // unicode 字符串类 // class unistring : public std::wstring { public: unistring() : std::wstring() {} unistring(const char* str) : std::wstring(to_unicode(str, strlen(str))) {} unistring(const char* str, size_t size) : std::wstring(to_unicode(str, size)) {} unistring(const wchar_t* str) : std::wstring(str) {} unistring(const wchar_t* str, size_t size) : std::wstring(str, size) {} // 整数转字符串 unistring(int n) : std::wstring(std::to_wstring(n)) {} // 单实数转字符串 unistring(float n) : std::wstring(std::to_wstring(n)) {} // 双实数转字符串 unistring(double n) : std::wstring(std::to_wstring(n)) {} // std::string unistring(const std::string& str) : std::wstring(to_unicode(str)) {} // std::wstring unistring(const std::wstring& str) : std::wstring(str) {} // 复制构造 unistring(const unistring& str) : std::wstring(str.c_str(), str.length()) {} // 赋值 unistring& operator=(const unistring& str) { std::wstring::assign(str.begin(), str.end()); return *this; } // 数值转换 int to_int()const { return to_numeric<int>(*this); } float to_float()const { return to_numeric<float>(*this); } double to_double()const { return to_numeric<double>(*this); } // 格式化字符串。只为简短信息设计,缓冲区大小 1024 unistring& format(const wchar_t* param, ...) { wchar_t buf[1024] = { 0 }; va_list body; va_start(body, param); _vsnwprintf(buf, 1024, param, body); va_end(body); this->assign(buf); return *this; } }; // 标准输出支持 inline std::ostream& operator << (std::ostream& out, const unistring& str) { out << to_ansi(str); return out; } #endif// UNISTRING_HPP
标签:11,std,const,string,C++,wstring,str,unistring From: https://www.cnblogs.com/sdragonx/p/16840325.html