/************************************************************************/ /* �ļ����ƣ� CharacterConvert.cpp �ļ���ʶ�� ����ժҪ�� ת�������ַ�����; ����˵���� �� ��ǰ�汾�� V 0.1 �� �ߣ� Jeff ������ڣ� 2015��04��05�� �ļ�¼1�� �����ڣ�- �� �� �ţ�- �� �� �ˣ�- �����ݣ�- */ /************************************************************************/ ////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CharacterConvert.h" ////////////////////////////////////////////////////////////////////////// /************************************************************************/ /* ����: utf82unicode ����: utf8�ַ���תΪunicode�ַ���; ����: pszutf8 utf8�ַ���; ����: unicode�ַ���; ע��: ���ص�ָ����ָ����ڴ���Ҫ�ͷ�; */ /************************************************************************/ WCHAR* utf82unicode(__in const char *pszutf8) { int wnSize = MultiByteToWideChar(CP_UTF8, 0, pszutf8, -1, NULL, 0); if (wnSize == ERROR_NO_UNICODE_TRANSLATION) { //throw std::exception("Invalide UTF-8 sequence"); return NULL; } if ( wnSize == 0 ) { //throw std::exception("Error in conversion"); return NULL; } WCHAR *pwResult = new WCHAR[wnSize]; int nConvertSize = MultiByteToWideChar(CP_UTF8, 0, pszutf8, -1, pwResult,wnSize); if ( nConvertSize != wnSize ) { //throw std::exception("la falla"); if(pwResult) delete pwResult; return NULL; } return pwResult; } /************************************************************************/ /* ����: utf82unicode ����: utf8�ַ���תΪunicode�ַ���; ����: pszutf8 utf8�ַ���; pszunicode ���ص�unicode�ַ���; ����: unicode�ַ�����С; */ /************************************************************************/ int utf82unicode(__in const char *pszutf8,__inout WCHAR *pszunicode) { int wnSize = MultiByteToWideChar(CP_UTF8, 0, pszutf8, -1, NULL, 0); if (wnSize == ERROR_NO_UNICODE_TRANSLATION) { //throw std::exception("Invalide UTF-8 sequence"); return 0; } if ( wnSize == 0 ) { //throw std::exception("Error in conversion"); return 0; } int nConvertSize = MultiByteToWideChar(CP_UTF8, 0, pszutf8, -1, pszunicode, wnSize); if ( nConvertSize != wnSize ) { //throw std::exception("la falla"); return 0; } return wnSize; } /************************************************************************/ /* ����: unicode2acsii ����: unicode�ַ���תΪacsii�ַ���; ����: pszunicode unicode�ַ���; ����: acsii�ַ���; ע��: ���ص�ָ����ָ����ڴ���Ҫ�ͷ�; */ /************************************************************************/ CHAR* unicode2acsii(__in const WCHAR *pszunicode) { int asciisize = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, NULL, 0, NULL, NULL); if (asciisize == ERROR_NO_UNICODE_TRANSLATION) { //throw std::exception("Invalid UTF-8 sequence."); return NULL; } if (asciisize == 0) { //throw std::exception("Error in conversion."); return NULL; } CHAR *pAscii = new CHAR[asciisize]; int convresult = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, pAscii, asciisize, NULL, NULL); if (convresult != asciisize) { //throw std::exception("La falla!"); if(pAscii) delete pAscii; return NULL; } return pAscii; } /************************************************************************/ /* ����: unicode2acsii ����: unicode�ַ���תΪacsii�ַ���; ����: pszunicode unicode�ַ���; pszacsii ���ص�acsii�ַ���; ����: acsii�ַ�����С; */ /************************************************************************/ int unicode2acsii(__in const WCHAR *pszunicode,__inout CHAR *pszacsii) { int asciisize = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, NULL, 0, NULL, NULL); if (asciisize == ERROR_NO_UNICODE_TRANSLATION) { //throw std::exception("Invalid UTF-8 sequence."); return 0; } if (asciisize == 0) { //throw std::exception("Error in conversion."); return 0; } int convresult = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, pszacsii, asciisize, NULL, NULL); if (convresult != asciisize) { //throw std::exception("La falla!"); return 0; } return asciisize; } /************************************************************************/ /* ����: utf82ascii ����: ��utf8�ַ���תΪascii�ַ���; ����: pszutf8 utf8�ַ���; ����: ascii�ַ���; ע��: ���ص�ָ����Ҫ�ֶ��ͷ���ָ�ڴ�; */ /************************************************************************/ CHAR* utf82ascii(const CHAR *pszutf8) { // �Ȱ� utf8 תΪ unicode ; WCHAR *pwstr = utf82unicode(pszutf8); // ���� unicode תΪ ascii ; CHAR *pacsii = NULL; if( pwstr ) pacsii = unicode2acsii(pwstr); if (pwstr) delete pwstr; return pacsii; } /************************************************************************/ /* ����: utf82ascii ����: ��utf8�ַ���תΪascii�ַ���; ����: pszutf8 utf8�ַ���; ����: ascii�ַ���; ע��: ���ص�ָ����Ҫ�ֶ��ͷ���ָ�ڴ�; */ /************************************************************************/ int utf82ascii(__in const CHAR *pszutf8, __inout CHAR* pszacsii) { // �Ȱ� utf8 תΪ unicode ; WCHAR *pwstr = utf82unicode(pszutf8); // ���� unicode תΪ ascii ; int nascii = 0; if( pwstr ) nascii = unicode2acsii(pwstr,pszacsii); if (pwstr) delete pwstr; return nascii; } /************************************************************************/ /* ����: unicode2uft8 ����: ��unicode�ַ���תΪutf8�ַ���; ����: pszunicode unicode�ַ���; ����: utf8�ַ���; ע��: ���ص�ָ����Ҫ�ֶ��ͷ���ָ�ڴ�; */ /************************************************************************/ CHAR* unicode2uft8(__in const WCHAR *pszunicode) { int utf8size = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, NULL, 0, NULL, NULL); if (utf8size == 0) { //throw std::exception("Error in conversion."); return NULL; } CHAR* putf8 = new CHAR[utf8size]; int convresult = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, putf8, utf8size, NULL, NULL); if (convresult != utf8size) { //throw std::exception("La falla!"); if(putf8)delete putf8; return NULL; } return putf8; } /************************************************************************/ /* ����: unicode2uft8 ����: ��unicode�ַ���תΪutf8�ַ���; ����: pszunicode unicode�ַ���; pszutf8 ���ص�utf8�ַ���; ����: utf8�ַ�����С; */ /************************************************************************/ int unicode2uft8(__in const WCHAR *pszunicode,__inout CHAR* pszutf8) { int utf8size = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, NULL, 0, NULL, NULL); if (utf8size == 0) { //throw std::exception("Error in conversion."); return 0; } int convresult = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, pszutf8, utf8size, NULL, NULL); if (convresult != utf8size) { //throw std::exception("La falla!"); return 0; } return utf8size; } /************************************************************************/ /* ����: ascii2unicode ����: ��ascii�ַ���תΪunicode�ַ���; ����: pszascii ascii�ַ���; ����: unicode�ַ���; ע��: ���ص�ָ����Ҫ�ֶ��ͷ�����ָ���ڴ�; */ /************************************************************************/ WCHAR* ascii2unicode(__in const CHAR* pszascii) { int wSize = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, NULL, 0); if (wSize == ERROR_NO_UNICODE_TRANSLATION) { //throw std::exception("Invalid UTF-8 sequence."); return NULL; } if (wSize == 0) { //throw std::exception("Error in conversion."); return NULL; } WCHAR *punicode = new WCHAR[wSize]; int convresult = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, punicode, wSize); if (convresult != wSize) { //throw std::exception("La falla!"); if(punicode) delete punicode; return NULL; } return punicode; } /************************************************************************/ /* ����: ascii2unicode ����: ��ascii�ַ���תΪunicode�ַ���; ����: pszascii ascii�ַ���; ����: unicode�ַ���; ע��: ���ص�ָ����Ҫ�ֶ��ͷ�����ָ���ڴ�; */ /************************************************************************/ int ascii2unicode(__in const CHAR* pszascii,__inout WCHAR *pszunicode) { int wSize = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, NULL, 0); if (wSize == ERROR_NO_UNICODE_TRANSLATION) { //throw std::exception("Invalid UTF-8 sequence."); return 0; } if (wSize == 0) { //throw std::exception("Error in conversion."); return 0; } int convresult = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, pszunicode, wSize); if (convresult != wSize) { //throw std::exception("La falla!"); return 0; } return wSize; } /************************************************************************/ /* ����: ascii2utf8 ����: ��ascii�ַ���תΪutf8�ַ���; ����: pszascii ascii�ַ���; ����: uft8�ַ���; ע��: ���ص�ָ����Ҫ�ֶ��ͷ�����ָ���ڴ�; */ /************************************************************************/ CHAR* ascii2utf8(__in const CHAR* pszascii) { // �Ȱ� ascii תΪ unicode ; WCHAR *pwstr = ascii2unicode(pszascii); // ���� unicode תΪ utf8 ; CHAR* putf8 = NULL; if (pwstr) putf8 = unicode2uft8(pwstr); if(pwstr) delete pwstr; return putf8; } /************************************************************************/ /* ����: ascii2utf8 ����: ��ascii�ַ���תΪutf8�ַ���; ����: pszascii ascii�ַ���; ����: uft8�ַ���; ע��: ���ص�ָ����Ҫ�ֶ��ͷ�����ָ���ڴ�; */ /************************************************************************/ int ascii2utf8(__in const CHAR* pszascii,__inout CHAR* pszutf8) { // �Ȱ� ascii תΪ unicode ; WCHAR *pwstr = ascii2unicode(pszascii); // ���� unicode תΪ utf8 ; int nSize = 0; if (pwstr) nSize = unicode2uft8(pwstr,pszutf8); if(pwstr) delete pwstr; return nSize; }