当前位置:   article > 正文

Unicode与UTF-8转换_c# unicode转义字符 utf-8

c# unicode转义字符 utf-8
  1. /*
  2. *Unicode转换成UTF-8
  3. *@param strUnicode: 待转换的CString
  4. *@param szUtf8:转换后的UTF-8
  5. *@return:返回UTF-8格式的长度
  6. */
  7. int UniToUTF8( wchar_t* pUniString, char *szUtf8 )
  8. {
  9. int nLen = WideCharToMultiByte(CP_UTF8, 0, pUniString, -1, NULL, 0, NULL, NULL);
  10. char *szUtf8Temp = new char[nLen + 1];
  11. memset( szUtf8Temp, 0, nLen + 1 );
  12. WideCharToMultiByte ( CP_UTF8, 0, pUniString, -1, szUtf8Temp, nLen, NULL, NULL );
  13. sprintf( szUtf8, "%s", szUtf8Temp );
  14. delete[] szUtf8Temp;
  15. return nLen;
  16. }
  17. int UniToUTF8( wchar_t* pUniString, std::string& strUtf8 )
  18. {
  19. int nLen = WideCharToMultiByte(CP_UTF8, 0, pUniString, -1, NULL, 0, NULL, NULL);
  20. char *szUtf8Temp = new char[nLen + 1];
  21. memset( szUtf8Temp, 0, nLen + 1 );
  22. WideCharToMultiByte ( CP_UTF8, 0, pUniString, -1, szUtf8Temp, nLen, NULL, NULL );
  23. strUtf8 = szUtf8Temp;
  24. delete[] szUtf8Temp;
  25. return nLen;
  26. }
  27. /*
  28. *UTF-8转换成Unicode
  29. *@param UTF8: 待转换的的UTF-8
  30. *@return:返回Unicode格式的字符串
  31. */
  32. CString UTF8ToUni( char* UTF8 )
  33. {
  34. DWORD dwUnicodeLen; //转换后Unicode的长度
  35. wchar_t *pwText; //保存Unicode的指针
  36. CString strUnicode; //返回值
  37. //获得转换后的长度,并分配内存
  38. dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,UTF8,-1,NULL,0);
  39. pwText = new wchar_t[dwUnicodeLen];
  40. if (!pwText)
  41. {
  42. return strUnicode;
  43. }
  44. //转为Unicode
  45. MultiByteToWideChar(CP_UTF8,0,UTF8,-1,pwText,dwUnicodeLen);
  46. //转为CString
  47. strUnicode.Format(_T("%s"),pwText);
  48. //清除内存
  49. delete []pwText;
  50. //返回转换好的Unicode字串
  51. return strUnicode;
  52. }
  53. CString UTF8ToUni( std::string& strUTF8 )
  54. {
  55. DWORD dwUnicodeLen; //转换后Unicode的长度
  56. wchar_t *pwText; //保存Unicode的指针
  57. CString strUnicode; //返回值
  58. //获得转换后的长度,并分配内存
  59. dwUnicodeLen = MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0 );
  60. pwText = new wchar_t[ dwUnicodeLen ];
  61. if ( !pwText ){
  62. return strUnicode;
  63. }
  64. //转为Unicode
  65. MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, pwText, dwUnicodeLen );
  66. strUnicode = pwText;
  67. //清除内存
  68. delete [] pwText;
  69. return strUnicode;
  70. }
  1. <pre name="code" class="cpp">void ConvertUtf8ToGBK(CString& strUtf8)
  2. {
  3. int len = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL, 0);
  4. unsigned short* wszGBK = new unsigned short[len + 1];
  5. memset(wszGBK, 0, len * 2 + 2);
  6. MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, (LPWSTR)wszGBK, len);
  7. len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
  8. char* szGBK = new char[len + 1];
  9. memset(szGBK, 0, len + 1);
  10. WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL, NULL);
  11. strUtf8 = szGBK;
  12. delete[] szGBK;
  13. delete[] wszGBK;
  14. }
  15. void ConvertGBKToUtf8(CString& strGBK)
  16. {
  17. int len = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL, 0);
  18. unsigned short* wszUtf8 = new unsigned short[len + 1];
  19. memset(wszUtf8, 0, len * 2 + 2);
  20. MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, (LPWSTR)wszUtf8, len);
  21. len = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
  22. char* szUtf8 = new char[len + 1];
  23. memset(szUtf8, 0, len + 1);
  24. WideCharToMultiByte (CP_UTF8, 0, (LPWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);
  25. strGBK = szUtf8;
  26. delete[] szUtf8;
  27. delete[] wszUtf8;
  28. }


 



                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/253409
推荐阅读
  

闽ICP备14008679号