CLewaimaiString.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. using namespace std;
  3. #include <Windows.h>
  4. class CLewaimaiString
  5. {
  6. public:
  7. CLewaimaiString();
  8. ~CLewaimaiString();
  9. static bool base64_encode(const string& input, string* output);
  10. static bool base64_decode(const string& input, string* output);
  11. static void trim(string &s);
  12. //将string转换成wstring
  13. static wstring string2wstring(string str)
  14. {
  15. wstring result;
  16. //获取缓冲区大小,并申请空间,缓冲区大小按字符计算
  17. int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
  18. TCHAR* buffer = new TCHAR[len + 1];
  19. //多字节编码转换成宽字节编码
  20. MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
  21. //添加字符串结尾
  22. buffer[len] = '\0';
  23. //删除缓冲区并返回值
  24. result.append(buffer);
  25. delete[] buffer;
  26. return result;
  27. }
  28. //将wstring转换成string
  29. static string wstring2string(wstring wstr)
  30. {
  31. string result;
  32. //获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
  33. int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
  34. char* buffer = new char[len + 1];
  35. //宽字节编码转换成多字节编码
  36. WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
  37. buffer[len] = '\0';
  38. //删除缓冲区并返回值
  39. result.append(buffer);
  40. delete[] buffer;
  41. return result;
  42. }
  43. };