utils_string.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "utils_string.h"
  2. #ifdef WIN32
  3. #include <Windows.h>
  4. #endif
  5. namespace am {
  6. std::wstring utils_string::ascii_unicode(const std::string &str)
  7. {
  8. int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
  9. wchar_t *pUnicode = (wchar_t *) malloc(sizeof(wchar_t) * unicodeLen);
  10. MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);
  11. std::wstring ret_str = pUnicode;
  12. free(pUnicode);
  13. return ret_str;
  14. }
  15. std::string utils_string::unicode_ascii(const std::wstring &wstr)
  16. {
  17. int ansiiLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
  18. char *pAssii = (char *) malloc(sizeof(char) * ansiiLen);
  19. WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
  20. std::string ret_str = pAssii;
  21. free(pAssii);
  22. return ret_str;
  23. }
  24. std::string utils_string::ascii_utf8(const std::string &str)
  25. {
  26. return unicode_utf8(ascii_unicode(str));
  27. }
  28. std::string utils_string::utf8_ascii(const std::string &utf8)
  29. {
  30. return unicode_ascii(utf8_unicode(utf8));
  31. }
  32. std::string utils_string::unicode_utf8(const std::wstring &wstr)
  33. {
  34. int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
  35. char *pAssii = (char *) malloc(sizeof(char) * ansiiLen);
  36. WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
  37. std::string ret_str = pAssii;
  38. free(pAssii);
  39. return ret_str;
  40. }
  41. std::wstring utils_string::utf8_unicode(const std::string &utf8)
  42. {
  43. int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, nullptr, 0);
  44. wchar_t *pUnicode = (wchar_t *) malloc(sizeof(wchar_t) * unicodeLen);
  45. MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, pUnicode, unicodeLen);
  46. std::wstring ret_str = pUnicode;
  47. free(pUnicode);
  48. return ret_str;
  49. }
  50. } // namespace am