utf8转ansi_utf8转ansi 易语言 汇编 - CSDN
精华内容
参与话题
  • C++ UTF8互转ANSI 支持char*和char[]和string wchar_t不能调用此函数(宽字符),请先...utf8转ansi 字符串=utf8_to_ansi(字符串) ansiutf8 字符串=ansi_to_utf8(字符串) char*和char[]:(注意:char *和char[]...

    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
    请按任意键继续. . .
    

    yuanwow


    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]
    
    

    yuanwow附 代码截图:
    yuanwow
    yuanwowyuanwow

    展开全文
  • UTF8<->ANSI转换函数

    万次阅读 2014-01-10 10:27:21
    void Convert(const char* strIn,char* strOut, int sourceCodepage, int targetCodepage) { int len=lstrlen(strIn); int unicodeLen=MultiByteToWideChar(sourceCodepage,0,strIn,-1,NULL,0);
     void Convert(const char* strIn,char* strOut, int sourceCodepage, int targetCodepage)
       {
           int len=lstrlen(strIn);
           int unicodeLen=MultiByteToWideChar(sourceCodepage,0,strIn,-1,NULL,0);
           wchar_t* pUnicode;
           pUnicode=new wchar_t[unicodeLen+1];
           memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
           MultiByteToWideChar(sourceCodepage,0,strIn,-1,(LPWSTR)pUnicode,unicodeLen);
           BYTE * pTargetData = NULL;
           int targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,0,NULL,NULL);
           pTargetData=new BYTE[targetLen+1];
           memset(pTargetData,0,targetLen+1);
           WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL);
           lstrcpy(strOut,(char*)pTargetData);
           delete pUnicode;
           delete pTargetData;
        }

    使用:Convert(strA_in,strB_out,CP_UTF8,CP_ACP)//UTF8转换ANSI

     Convert(strA_out,strB_in,CP_ACP,CP_UTF8)//ANSI转换UTF8


    http://bbs.csdn.net/topics/390253679

    展开全文
  • UTF-8转换为ANSI

    2020-02-14 19:13:27
    使用OCR识别出来的字符为utf-8编码的,如果直接用C/C++中的cout或printf打印,中文会显示乱码的,因此要先将utf-8编码转换为C/C++中的ANSI编码,然后再用cout或printf打印即可。 wchar_t *utf_8ToUnicode(char *u8s)...

    使用OCR识别出来的字符为utf-8编码的,如果直接用C/C++中的cout或printf打印,中文会显示乱码的,因此要先将utf-8编码转换为C/C++中的ANSI编码,然后再用cout或printf打印即可。

    #include<leptonica\allheaders.h>
    #include<tesseract\baseapi.h>
    #include<iostream>
    #include<time.h>
    using namespace std;
    
    wchar_t *utf_8ToUnicode(char *u8s)
    {
    	int wcsLen = MultiByteToWideChar(CP_UTF8, NULL, u8s, strlen(u8s), NULL, NULL);
    	wchar_t *wcString = new wchar_t[wcsLen + 1];
    	MultiByteToWideChar(CP_UTF8, NULL, u8s, strlen(u8s), wcString, wcsLen);
    	wcString[wcsLen] = '\0';
    	return wcString;
    }
    char *unicodeToAnsi(wchar_t *wcString)
    {
    	int len = WideCharToMultiByte(CP_ACP, NULL, wcString, -1, NULL, NULL, NULL, NULL);
    	char *str = new char[len];
    	WideCharToMultiByte(CP_ACP, NULL, wcString, -1, str, len, NULL, NULL);
    	return str;
    }
    
    int main()
    {
    	clock_t start = clock();
    
    	tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI;
    	/*参数1为存放traineddata文件的文件夹tessdata的路径,如果已经在环境变量那里设置好了,
    	  那可以把它设置为NULL;参数2为字库文件名,chi_sim代表简体中文,chi_tra代表繁体
    	  中文,eng代表英文*/
    	if (api->Init(NULL, "chi_sim"))
    		exit(1);
    	Pix *image = pixRead("2.png");
    	api->SetImage(image);
    	char *outText = api->GetUTF8Text();
    	wchar_t *aaa = utf_8ToUnicode(outText);
    	outText = unicodeToAnsi(aaa);
    
    	clock_t end = clock();
    
    	cout << "OCR output:\n" << outText;
    	cout << end - start << " ms" << endl;
    
    	delete outText;
    	delete api;
    	return 0;
    }
    
    展开全文
  • UTF-8ANSI 编码转换

    千次阅读 2010-06-11 18:45:00
    平时Coding的时候,经常会遇到一些编码转换的问题,尤其是UTF-8ANSI 编码转换的问题。遇到好多回了,所以不得不拿出来单独看一下。其实这个问题如果知道怎么做的话也不是问题,但是第一次接触到的时候我却是费了好...

    平时Coding的时候,经常会遇到一些编码转换的问题,尤其是UTF-8与ANSI 编码转换的问题。遇到好多回了,所以不得不拿出来单独看一下。其实这个问题如果知道怎么做的话也不是问题,但是第一次接触到的时候我却是费了好大的功夫才搞出来!

    看一下下面的代码

     

    其中两个主要的函数为:

    int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cchMultiByte, LPWSTR lpWideCharStr, int cchWideChar);

    函数功能:该函数映射一个字符串到一个宽字符(unicode)的字符串。由该函数映射的字符串没必要是多字节字符组。

    int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPWSTR lpWideCharStr, int cchWideChar, LPCSTR lpMultiByteStr, int cchMultiByte, LPCSTR lpDefaultChar, PBOOL pfUsedDefaultChar );

    函数功能:该函数映射一个unicode字符串到一个多字节字符串。

    具体参数百度百科讲的很明白,看一下就知道怎么回事了,我就不赘述了。

    其实就是这么点问题,但是自己解决的时候却费了很大的力气,好处就是如果别的事情我可以我可以忘掉,但是这个我真的再也忘不掉了!

     

    展开全文
  • UTF-8转ANSI文本文件转换器

    热门讨论 2020-07-30 23:30:53
    可批量将UTF-8的文本文件转为ANSI文本文件,代码略加修改,即可在换任意两种编码的文本文件内互相转化。
  • 字符串在通讯传输过程中,尤其是CS客户端-服务器通讯ANSIUTF8的转换...1 ANSI转UTF8 void ConvertANSIToUTF8(CString &amp;strANSI) { int nLen = ::MultiByteToWideChar(CP_ACP,MB_ERR_INVALID_CHARS,(...
  • 1 #include &lt;winnls.h&gt; 2 #include &... 3 LPCSTR AnsiToUtf8(LPCSTR Ansi) 4 { 5 int WLength = MultiByteToWideChar(CP_ACP, 0, Ansi, -1, NULL, 0); 6 LPWSTR pszW = (LPWSTR...
  • ANSI,UNICODE,UTF8之间的相互转换

    千次阅读 2018-12-03 19:25:53
    ANSI,UNICODE,UTF8之间的相互转换ANSI转UNICOEUNICODE转ANSIUTF8转UNICODEUNICODE转UTF8ANSIUTF8UTF8转ANSI ANSI转UNICOE std::wstring AnsiToUnicode(const std::string &amp;amp;amp;amp;strAnsi) { ...
  • ANSI转化为UTF-8
  • ANSI编码方式转化为UTF-8方式

    千次阅读 2014-12-29 19:17:51
    记事本txt有四种编码方式,分别为:UTF-8ANSI、Unicode和Unicode big endian,当进行写操作,创建的txt编码格式,与写入汉字的编码方式相同;如果写入的汉字是不同的编码方式,此时创建的txt中,会出现乱码,所以...
  • 场景: 1.也就只有windows需要...2.有时候需要把ansi文件内容转换为utf8编码,读取一行之后,把ansi字符串转换为utf8,之后写入文件。 #include #include #include #include #include char* Unicode2Utf8(const
  • ANSI转UTF8编码

    千次阅读 2011-08-04 23:16:22
    转换思路如下 1.ANSI成UNICODE 2.UNICODEUTF8 下面代码是直接由UNICODE转为UTF8编码
  • 使用Trados2011翻译英文html后,如果是单个文件,可在另存译文时选择Encoding为utf8,但是项目文件导出时却没有选项可以实现编码的转换。   方案:可使用Notepad++实现   选择Plugins->Plugin ...
  • .java文件的ANSI编码转换为UTF-8编码

    万次阅读 2012-09-07 17:07:31
    .java文件的ANSI编码转换为UTF-8编码,如下:   /* * 创建于:2012-09-07 * * 功能:转换ANSI【JAVA中GBK编码】编码的.java源文件为UTF-8格式,未处理异常,运行前确认目录存在,且原编码格式为ANSI * * @...
  • 关于ANSI,unicode与utf-8的区别

    万次阅读 2018-07-12 10:14:02
    关于ANSI,unicode与utf-8的区别为使计算机支持更多语言,通常使用 0x80~0xFFFF 范围的 2 个字节来表示 1 个字符。比如:汉字 '中' 在ANSI编码中文操作系统中,使用 [0xD6,0xD0] 这两个字节存储。不同的国家和地区...
  • conv -c -f GB2312 -t UTF-8 [你要看的文件] &gt; [新文件的名称] 按回车,即可在相同目录下出现新命名的文件名,该文件已完成指定编码转换。 举个栗子~比如想要将test1.cpp的乱码文件转换为test2.cpp(新定义...
  • ansi编码和utf8相互转换

    千次阅读 2019-05-31 17:47:37
    1、windows平台下 ...int CParserIni::ansi2utf8(const string& ansiStr, string& utf8Str) { int ret = kNoError; do{ //CP_ACP(ANSI字符集) if (ansiStr.empty()) BREAK_WITH_ERROR(kInvalidParamete...
  • 一般程序猿们开始码字的时候,习惯了新建PHP或者...下面就是本文要说的方法了:1、在桌面新建一个文本文档,不要写入任何问题,然后手动另存为,将此文档编码改为UTF-8,然后将文件名字改为template.txt(文件名可以随意
  • php 字符编码转换类,支持ANSI、Unicode、Unicode big endian、UTF-8UTF-8+Bom 互相转换。
  • @批量txt文本编码,由utf-8转为ANSI(ASCII) 废话不多说,直接上代码,这是java的技术;之前在网上找了好久都没有多大作用,老是出现乱码;找不到原因,现在可以了;编码可以实现了! 具体代码如下: // 代码部门 //默认的...
1 2 3 4 5 ... 20
收藏数 39,006
精华内容 15,602
关键字:

utf8转ansi