123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- #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.");
- }
- if (asciisize == 0)
- {
- throw std::exception("Error in conversion.");
- }
- int convresult = WideCharToMultiByte(CP_OEMCP, 0, pszunicode, -1, pszacsii, asciisize, NULL, NULL);
- if (convresult != asciisize)
- {
- throw std::exception("La falla!");
- }
- 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.");
- }
- int convresult = WideCharToMultiByte(CP_UTF8, 0, pszunicode, -1, pszutf8, utf8size, NULL, NULL);
- if (convresult != utf8size)
- {
- throw std::exception("La falla!");
- }
- 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.");
- }
- if (wSize == 0)
- {
- throw std::exception("Error in conversion.");
- }
- int convresult = MultiByteToWideChar (CP_ACP, 0, pszascii, -1, pszunicode, wSize);
- if (convresult != wSize)
- {
- throw std::exception("La falla!");
- }
- 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;
- }
- //wchar* to char*
- int WChar2Char(__inout char* pDest, __in const wchar_t* pSource)
- {
- if(pSource == NULL || pDest == NULL)
- return -1;
- int nLen = ::WideCharToMultiByte(CP_ACP, NULL, pSource, wcslen(pSource), NULL, 0, NULL, NULL);
- // Unicode版对应的strlen是wcslen
- ::WideCharToMultiByte(CP_ACP, NULL, pSource, wcslen(pSource), pDest, nLen, NULL, NULL);
- // 最后加上'\0'
- pDest[nLen] = '\0';
- return nLen;
- }
- //char* to wchar*
- int Char2WChar(__inout wchar_t* pDest, __in const char* pSource)
- {
- if(pSource == NULL || pDest == NULL)
- return -1;
- int nLen = ::MultiByteToWideChar(CP_ACP, 0, pSource, -1, NULL, 0);
- ::MultiByteToWideChar(CP_ACP, 0, pSource, -1, pDest, nLen);
- return nLen;
- }
|