C++ UTF8互转ANSI 支持char*和char[]和string
这应该是我第一篇字数上万的文章吧。
2019/08/07增补:
发现一个重大错误: 调用wchar_t版本的函数会崩溃,即运行时会突然弹出错误框然后终止运行!
现在此函数已停用(注释掉了)
解决方案:请先转换string或char再运行函数之后转换回来
调用方法:
使用重载方式一个函数多种调用方法
string:
utf8转ansi 字符串=utf8_to_ansi(字符串)
ansi转utf8 字符串=ansi_to_utf8(字符串)
char*和char[]:(注意:char *和char[]不能直接赋值,请通过strcpy赋值)
utf8转ansi strcpy(字符串,utf8_to_ansi(字符串))
ansi转utf8 strcpy(字符串,ansi_to_utf8(字符串))
c++函数实现
/*
#include<string>
#include<windows.h>
using namespace std;
//这些函数需包含这些头文件
*/
string utf8_to_ansi(string);
string ansi_to_utf8(string);
char* utf8_to_ansi(char*);
char* ansi_to_utf8(char*);//这个需要重载!不能少
/*
int main() {
//可放一些代码..
}
*/
string utf8_to_ansi(string strUTF8) {
UINT nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,NULL,NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,wszBuffer,nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
CHAR *szBuffer = new CHAR[nLen+1];
nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
szBuffer[nLen] = 0;
strUTF8 = szBuffer;
delete []szBuffer;
delete []wszBuffer;
return strUTF8;
}
string ansi_to_utf8(string strAnsi) {
UINT nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,NULL,NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,wszBuffer,nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
CHAR *szBuffer = new CHAR[nLen+1];
nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
szBuffer[nLen] = 0;
strAnsi = szBuffer;
delete []wszBuffer;
delete []szBuffer;
return strAnsi;
}
char* utf8_to_ansi(char* strUTF8A) {
string strUTF8=strUTF8A;
UINT nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,NULL,NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,wszBuffer,nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
CHAR *szBuffer = new CHAR[nLen+1];
nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
szBuffer[nLen] = 0;
strUTF8 = szBuffer;
delete []szBuffer;
delete []wszBuffer;
char* p=(char*)strUTF8.data();
return p;
}
char* ansi_to_utf8(char* strAnsiA) {
string strAnsi=strAnsiA;
UINT nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,NULL,NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,wszBuffer,nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
CHAR *szBuffer = new CHAR[nLen+1];
nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
szBuffer[nLen] = 0;
strAnsi = szBuffer;
delete []wszBuffer;
delete []szBuffer;
char* p=(char*)strAnsi.data();
return p;
}
//wchar_t* utf8_to_ansi(wchar_t* strUTF8A) {
// wchar_t * wText = strUTF8A;
// DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);
// char *psText;
// psText = new char[dwNum];
// WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);
// string strUTF8(psText);
// delete []psText;
// UINT nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,NULL,NULL);
// WCHAR *wszBuffer = new WCHAR[nLen+1];
// nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,wszBuffer,nLen);
// wszBuffer[nLen] = 0;
// nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
// CHAR *szBuffer = new CHAR[nLen+1];
// nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
// szBuffer[nLen] = 0;
// strUTF8 = szBuffer;
// delete []szBuffer;
// delete []wszBuffer;
// wchar_t* p;
// swprintf(p,L"%S",strUTF8.c_str());
// return p;
//}
//wchar_t* ansi_to_utf8(wchar_t* strAnsiA) {
// wchar_t * wText = strAnsiA;
// DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);
// char *psText;
// psText = new char[dwNum];
// WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);
// string strAnsi(psText);
// delete []psText;
// UINT nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,NULL,NULL);
// WCHAR *wszBuffer = new WCHAR[nLen+1];
// nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,wszBuffer,nLen);
// wszBuffer[nLen] = 0;
// nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
// CHAR *szBuffer = new CHAR[nLen+1];
// nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
// szBuffer[nLen] = 0;
// strAnsi = szBuffer;
// delete []wszBuffer;
// delete []szBuffer;
// wchar_t* p;
// swprintf(p,L"%S",strAnsi.c_str());
// return p;
//}
c函数实现:
看标题就知道不可能的
使用实例:
c++函数:
#include<string>
#include<windows.h>
#include<iostream>
using namespace std;
string utf8_to_ansi(string);
string ansi_to_utf8(string);
char* utf8_to_ansi(char*);
char* ansi_to_utf8(char*);
//wchar_t* utf8_to_ansi(wchar_t*);
//wchar_t* ansi_to_utf8(wchar_t*);
int main() {
do {
string a;
cout<<"\nstring字符串\n";
a="鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾";
cout<<"原始字符串:\n"<<a;
a=utf8_to_ansi(a);
cout<<"\nutf8_to_ansi:\n"<<a;
a=ansi_to_utf8(a);
cout<<"\nansi_to_utf8:\n"<<a;
} while(0);
cout<<endl;
do {
char a[1000];
cout<<"\nchar[]/char*字符串\n";
strcpy(a,"鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾");
cout<<"原始字符串:\n"<<a;
strcpy(a,utf8_to_ansi(a));
cout<<"\nutf8_to_ansi:\n"<<a;
strcpy(a,ansi_to_utf8(a));
cout<<"\nansi_to_utf8:\n"<<a;
} while(0);
//----------标记 ---- wchar_t 测试失败
// do {
// wchar_t a[1000];
// cout<<"\nchar[]/char*字符串\n";
// strcpy(a,"鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾");
// cout<<"src utf8:"<<a;
// wcscpy(a,utf8_to_ansi(a));
// cout<<"\nutf8_to_ansi:"<<a;
// strcpy(a,ansi_to_utf8(a));
// cout<<"\nansi_to_utf8:"<<a;
// } while(0);
}
string utf8_to_ansi(string strUTF8) {
UINT nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,NULL,NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,wszBuffer,nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
CHAR *szBuffer = new CHAR[nLen+1];
nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
szBuffer[nLen] = 0;
strUTF8 = szBuffer;
delete []szBuffer;
delete []wszBuffer;
return strUTF8;
}
string ansi_to_utf8(string strAnsi) {
UINT nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,NULL,NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,wszBuffer,nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
CHAR *szBuffer = new CHAR[nLen+1];
nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
szBuffer[nLen] = 0;
strAnsi = szBuffer;
delete []wszBuffer;
delete []szBuffer;
return strAnsi;
}
char* utf8_to_ansi(char* strUTF8A) {
string strUTF8=strUTF8A;
UINT nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,NULL,NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,wszBuffer,nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
CHAR *szBuffer = new CHAR[nLen+1];
nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
szBuffer[nLen] = 0;
strUTF8 = szBuffer;
delete []szBuffer;
delete []wszBuffer;
char* p=(char*)strUTF8.data();
return p;
}
char* ansi_to_utf8(char* strAnsiA) {
string strAnsi=strAnsiA;
UINT nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,NULL,NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,wszBuffer,nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
CHAR *szBuffer = new CHAR[nLen+1];
nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
szBuffer[nLen] = 0;
strAnsi = szBuffer;
delete []wszBuffer;
delete []szBuffer;
char* p=(char*)strAnsi.data();
return p;
}
//wchar_t* utf8_to_ansi(wchar_t* strUTF8A) {
// wchar_t * wText = strUTF8A;
// DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);
// char *psText;
// psText = new char[dwNum];
// WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);
// string strUTF8(psText);
// delete []psText;
// UINT nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,NULL,NULL);
// WCHAR *wszBuffer = new WCHAR[nLen+1];
// nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8.c_str(),-1,wszBuffer,nLen);
// wszBuffer[nLen] = 0;
// nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
// CHAR *szBuffer = new CHAR[nLen+1];
// nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
// szBuffer[nLen] = 0;
// strUTF8 = szBuffer;
// delete []szBuffer;
// delete []wszBuffer;
// wchar_t* p;
// swprintf(p,L"%S",strUTF8.c_str());
// return p;
//}
//wchar_t* ansi_to_utf8(wchar_t* strAnsiA) {
// wchar_t * wText = strAnsiA;
// DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);
// char *psText;
// psText = new char[dwNum];
// WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);
// string strAnsi(psText);
// delete []psText;
// UINT nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,NULL,NULL);
// WCHAR *wszBuffer = new WCHAR[nLen+1];
// nLen = MultiByteToWideChar(936,NULL,strAnsi.c_str(),-1,wszBuffer,nLen);
// wszBuffer[nLen] = 0;
// nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
// CHAR *szBuffer = new CHAR[nLen+1];
// nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
// szBuffer[nLen] = 0;
// strAnsi = szBuffer;
// delete []wszBuffer;
// delete []szBuffer;
// wchar_t* p;
// swprintf(p,L"%S",strAnsi.c_str());
// return p;
//}
这里的do{/*代码*/}while(0);
是为了防止变量名重复
运行结果:
string字符串
原始字符串:
鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾
utf8_to_ansi:
百度一下,你就知道
ansi_to_utf8:
鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾
char[]/char*字符串
原始字符串:
鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾
utf8_to_ansi:
百度一下,你就知道
ansi_to_utf8:
鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾
--------------------------------
Process exited after 0.05147 seconds with return value 0
请按任意键继续. . .
Dev cpp编译通过
附 编译过程:
(我的文件是D:\Documents\未命名asdasd5.cpp不要太在意了)
放心都是警告不是错误
D:\Documents\未命名asdasd5.cpp In function 'std::string utf8_to_ansi(std::string)':
46 75 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
46 75 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 6 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
48 75 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
50 70 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
50 70 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 6 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
52 74 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
D:\Documents\未命名asdasd5.cpp In function 'std::string ansi_to_utf8(std::string)':
60 71 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
60 71 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 6 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
62 71 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
64 74 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
64 74 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 6 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
66 78 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
D:\Documents\未命名asdasd5.cpp In function 'char* utf8_to_ansi(char*)':
75 75 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
75 75 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 6 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
77 75 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
79 70 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
79 70 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 6 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
81 74 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
D:\Documents\未命名asdasd5.cpp In function 'char* ansi_to_utf8(char*)':
91 71 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
91 71 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 6 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
93 71 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int MultiByteToWideChar(UINT, DWORD, LPCCH, int, LPWSTR, int)' [-Wconversion-null]
95 74 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
95 74 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 6 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
97 78 D:\Documents\未命名asdasd5.cpp [Warning] passing NULL to non-pointer argument 2 of 'int WideCharToMultiByte(UINT, DWORD, LPCWCH, int, LPSTR, int, LPCCH, LPBOOL)' [-Wconversion-null]
附 代码截图: