1. UTF-8Unicode
    2. std::wstring Utf82Unicode(const std::string& utf8string)
    3. {
    4. int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
    5. if (widesize == ERROR_NO_UNICODE_TRANSLATION)
    6. {
    7. throw std::exception("Invalid UTF-8 sequence.");
    8. }
    9. if (widesize == 0)
    10. {
    11. throw std::exception("Error in conversion.");
    12. }
    13. std::vector<wchar_t> resultstring(widesize);
    14. int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);
    15. if (convresult != widesize)
    16. {
    17. throw std::exception("La falla!");
    18. }
    19. return std::wstring(&resultstring[0]);
    20. }
    21. Unicode 转为 Ascii
    22. string WideByte2Acsi(wstring& wstrcode)
    23. {
    24. int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
    25. if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
    26. {
    27. throw std::exception("Invalid UTF-8 sequence.");
    28. }
    29. if (asciisize == 0)
    30. {
    31. throw std::exception("Error in conversion.");
    32. }
    33. std::vector<char> resultstring(asciisize);
    34. int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);
    35. if (convresult != asciisize)
    36. {
    37. throw std::exception("La falla!");
    38. }
    39. return std::string(&resultstring[0]);
    40. }
    41. UTF-8 Ascii
    42. string UTF_82ASCII(string& strUtf8Code)
    43. {
    44. string strRet("");
    45. //先把 utf8 转为 unicode
    46. wstring wstr = Utf82Unicode(strUtf8Code);
    47. //最后把 unicode 转为 ascii
    48. strRet = WideByte2Acsi(wstr);
    49. return strRet;
    50. }
    51. Ascii Unicode
    52. wstring Acsi2WideByte(string& strascii)
    53. {
    54. int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
    55. if (widesize == ERROR_NO_UNICODE_TRANSLATION)
    56. {
    57. throw std::exception("Invalid UTF-8 sequence.");
    58. }
    59. if (widesize == 0)
    60. {
    61. throw std::exception("Error in conversion.");
    62. }
    63. std::vector<wchar_t> resultstring(widesize);
    64. int convresult = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
    65. if (convresult != widesize)
    66. {
    67. throw std::exception("La falla!");
    68. }
    69. return std::wstring(&resultstring[0]);
    70. }
    71. Unicode UTF8
    72. std::string Unicode2Utf8(std::wstring& widestring)
    73. {
    74. using namespace std;
    75. int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
    76. if (utf8size == 0)
    77. {
    78. throw std::exception("Error in conversion.");
    79. }
    80. std::vector<char> resultstring(utf8size);
    81. int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);
    82. if (convresult != utf8size)
    83. {
    84. throw std::exception("La falla!");
    85. }
    86. return std::string(&resultstring[0]);
    87. }
    88. ————————————————
    89. 版权声明:本文为CSDN博主「Just_like_fire」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    90. 原文链接:https://blog.csdn.net/Leo_csdn_/article/details/100761644