/************************************************************************/ /* 文件名称: 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; }