C++字符转换等常用方法

下面是C++中字符编码格式转换(包括ANSI转换为UTF8,UTF8转换为ANSI,ANSI转换为unicode,unicode转换为ANSI,UTF8转换为unicode,unicode转换为UTF8),大小写转换等常用方法,写在这儿,免得忘掉。

头文件:

  1 #pragma once
  2 
  3 #include <string>
  4 #include <vector>
  5 
  6 namespace Hi
  7 {
  8     bool replace(std::string& source, const char* old, const char* newStr);
  9 
 10     void toLower(std::string& source);
 11 
 12     void toUpper(std::string& source);
 13 
 14     /**
 15     *   @brief 将utf-8编码的字符串转换为unicode编码的字符串。
 16     *   @param[in] str    utf-8编码的字符串
 17     *   @param[out] content    unicode编码的字符串
 18     *   @retval true:成功,false;失败
 19     */
 20     bool u82uc(const char* str, std::wstring& content);
 21 
 22     /**
 23     *   @brief 将unicode编码的字符串转换为utf-8编码的字符串。
 24     *   @param[in] str    unicode编码的字符串
 25     *   @param[out] content    utf-8编码的字符串
 26     *   @retval true:成功,false;失败
 27     */
 28     bool uc2u8(const wchar_t* str, std::string& content);
 29 
 30     /**
 31     *   @brief 将unicode编码的字符串转换为ansi编码的字符串。
 32     *   @param[in] str    unicode编码的字符串
 33     *   @param[out] content    ansi编码的字符串
 34     *   @retval true:成功,false;失败
 35     */
 36     bool uc2as(const wchar_t* str, std::string& content);
 37 
 38     /**
 39     *   @brief 将ansi编码的字符串转换为unicode编码的字符串。
 40     *   @param[in] str    ansi编码的字符串
 41     *   @param[out] content    unicode编码的字符串
 42     *   @retval true:成功,false;失败
 43     */
 44     bool as2uc(const char* str, std::wstring& content);
 45 
 46     /**
 47     *   @brief 将utf-8编码的字符串转换为ansi编码的字符串。
 48     *   @param[in] str    utf-8编码的字符串
 49     *   @param[out] content    ansi编码的字符串
 50     *   @retval true:成功,false;失败
 51     */
 52     bool u82as(const char* str, std::string& content);
 53 
 54     /**
 55     *   @brief 将utf-8编码的字符串转换为unicode编码的字符串。
 56     *   @param[in] str    utf-8编码的字符串
 57     *   @param[out] content    unicode编码的字符串
 58     *   @retval true:成功,false;失败
 59     */
 60     bool as2u8(const char* str, std::string& content);
 61 }
 62 namespace Hi
 63 {
 64     namespace Ex
 65     {
 66         /**
 67         *   @brief 将utf-8编码的字符串转换为unicode编码的字符串。
 68         *   @param[in] str    utf-8编码的字符串
 69         *   @retval unicode编码的字符串
 70         *   @note 如果失败,返回""
 71         */
 72         std::wstring u82uc(const char* str);
 73 
 74         /**
 75         *   @brief 将unicode编码的字符串转换为utf-8编码的字符串。
 76         *   @param[in] str    ansi编码的字符串
 77         *   @retval utf-8编码的字符串
 78         *   @note 如果失败,返回""
 79         */
 80         std::string uc2u8(const wchar_t* str);
 81 
 82         /**
 83         *   @brief 将utf-8编码的字符串转换为unicode编码的字符串。
 84         *   @param[in] str    utf-8编码的字符串
 85         *   @retval unicode编码的字符串
 86         *   @note 如果失败,返回""
 87         */
 88         std::string uc2as(const wchar_t* str);
 89 
 90         /**
 91         *   @brief 将ansi编码的字符串转换为unicode编码的字符串。
 92         *   @param[in] str    ansi编码的字符串
 93         *   @retval unicode编码的字符串
 94         *   @note 如果失败,返回""
 95         */
 96         std::wstring as2uc(const char* str);
 97 
 98         /**
 99         *   @brief 将utf-8编码的字符串转换为ansi编码的字符串。
100         *   @param[in] str    utf-8编码的字符串
101         *   @retval ansi编码的字符串
102         *   @note 如果失败,返回""
103         */
104         std::string u82as(const char* str);
105 
106         /**
107         *   @brief 将ansi编码的字符串转换为utf-8编码的字符串。
108         *   @param[in] str    ansi编码的字符串
109         *   @retval utf-8编码的字符串
110         *   @note 如果失败,返回""
111         */
112         std::string as2u8(const char* str);
113     }
114 }

源文件:

  1 #include "stdafx.h"
  2 
  3 #include <functional>
  4 #include <algorithm>
  5 
  6 #include "HiString.h"
  7 
  8 using namespace std;
  9 
 10 namespace Hi
 11 {
 12     static bool replace_if_pr_2(char itor, char old)
 13     {
 14         if (itor == old)
 15         {
 16             return true;
 17         }
 18         return false;
 19     }
 20 
 21     bool replace(string& source, const char* old, const char* newStr)
 22     {
 23         if (::strlen(old) == 1 &&  ::strlen(newStr) == 1)
 24         {
 25             replace_if(source.begin(), source.end(), 
 26                 bind2nd(ptr_fun(replace_if_pr_2), *old), *newStr);
 27 
 28             return true;
 29         }
 30 
 31         string s1(old);
 32         string s2(newStr);
 33 
 34         if (s1 == s2)
 35         {
 36             return true;
 37         }
 38         string::size_type pos = source.find(s1);
 39         while (pos != string::npos)
 40         {
 41             source.replace(pos, s1.size(), s2);
 42             pos = source.find(old);
 43         }
 44 
 45         return true;
 46     }
 47 
 48     void toLower(string& source)
 49     {
 50          transform(source.begin(), source.end(), source.begin(), tolower);  
 51     }
 52 
 53     void toUpper(string& source)
 54     {
 55         transform(source.begin(), source.end(), source.begin(), toupper);  
 56     }
 57     bool u82uc(const char* str, wstring& content)
 58     {
 59         int textlen ;
 60         wchar_t * result;
 61         textlen = MultiByteToWideChar( CP_UTF8, 0, str,-1, NULL,0 ); 
 62         result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); 
 63         ::memset(result,0,(textlen+1)*sizeof(wchar_t)); 
 64         MultiByteToWideChar(CP_UTF8, 0,str,-1,(LPWSTR)result,textlen); 
 65 
 66         content = result;
 67         free(result);
 68 
 69         return true; 
 70     }
 71 
 72     bool uc2as(const wchar_t* str, string& content)
 73     {
 74         char* result;
 75         int textlen;
 76         textlen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
 77         result =(char *)malloc((textlen+1)*sizeof(char));
 78         memset( result, 0, sizeof(char) * ( textlen + 1 ) );
 79         WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL );
 80 
 81         content = result;
 82         free(result);
 83 
 84         return true; 
 85     }
 86 
 87     bool u82as(const char* str, string& content)
 88     {
 89         wstring temp;
 90         u82uc(str, temp);
 91         return uc2as(temp.c_str(), content);
 92     }
 93 
 94     bool as2uc(const char* str, wstring& content)
 95     {
 96         int textlen ;
 97         wchar_t * result;
 98         textlen = MultiByteToWideChar(CP_ACP, 0, str,-1, NULL,0 ); 
 99         result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); 
100         ::memset(result,0,(textlen+1)*sizeof(wchar_t)); 
101         MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen); 
102 
103         content = result;
104         free(result);
105         return true; 
106     }
107 
108     bool uc2u8(const wchar_t* str, string& content)
109     {
110         char* result;
111         int textlen;
112         textlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
113         result =(char *)malloc((textlen+1)*sizeof(char));
114         memset( result, 0, sizeof(char) * ( textlen + 1 ) );
115         WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL );
116 
117         content = result;
118         free(result);
119 
120         return true; 
121     }
122 
123     bool as2u8(const char* str, string& content)
124     {
125         wstring temp;
126         as2uc(str, temp);
127         return uc2u8(temp.c_str(), content);
128     }
129 }
130 
131 
132 namespace Hi
133 {
134     namespace Ex
135     {
136         wstring u82uc(const char* str)
137         {
138             wstring temp;
139             if (Hi::u82uc(str, temp))
140             {
141                 return temp;
142             }
143 
144             return L"";
145         }
146 
147         string uc2as(const wchar_t* str)
148         {
149             string temp;
150             if (Hi::uc2as(str, temp))
151             {
152                 return temp;
153             }
154 
155             return "";
156         }
157 
158         string u82as(const char* str)
159         {
160             string temp;
161             if (Hi::u82as(str, temp))
162             {
163                 return temp;
164             }
165 
166             return "";
167         }
168 
169         string as2u8(const char* str)
170         {
171             string temp;
172             if (Hi::as2u8(str, temp))
173             {
174                 return temp;
175             }
176 
177             return "";
178         }
179 
180         wstring as2uc(const char* str)
181         {
182             wstring temp;
183             if (Hi::as2uc(str, temp))
184             {
185                 return temp;
186             }
187 
188             return L"";
189         }
190 
191         string uc2u8(const wchar_t* str)
192         {
193             string temp;
194             if (Hi::uc2u8(str, temp))
195             {
196                 return temp;
197             }
198 
199             return "";
200         }
201     }
202 }
原文地址:https://www.cnblogs.com/admin11/p/3737146.html