Encoding.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //------------------------------------------------------------------------------
  2. //
  3. // Copyright (C) Streamlet. All rights reserved.
  4. //
  5. // File Name: Encoding.cpp
  6. // Author: Streamlet
  7. // Create Time: 2010-09-15
  8. // Description:
  9. //
  10. // Version history:
  11. //
  12. //
  13. //
  14. //------------------------------------------------------------------------------
  15. #include "stdafx.h"
  16. #include "Encoding.h"
  17. CStringW ANSIToUCS2(const CStringA &strANSI, UINT uCodePage /*= CP_ACP*/)
  18. {
  19. int size = MultiByteToWideChar(uCodePage, 0, strANSI, -1, NULL, 0);
  20. if (size == 0)
  21. {
  22. return L"";
  23. }
  24. WCHAR *szUCS2 = new WCHAR[size];
  25. if (MultiByteToWideChar(uCodePage, 0, strANSI, -1, szUCS2, size) == 0)
  26. {
  27. delete[] szUCS2;
  28. return L"";
  29. }
  30. CStringW ret = szUCS2;
  31. delete[] szUCS2;
  32. return ret;
  33. }
  34. CStringA UCS2ToANSI(const CStringW &strUCS2, UINT uCodePage /*= CP_ACP*/)
  35. {
  36. int size = WideCharToMultiByte(uCodePage, 0, strUCS2, -1, NULL, 0, NULL, NULL);
  37. if (size == 0)
  38. {
  39. return "";
  40. }
  41. CHAR *szANSI = new CHAR[size];
  42. if (WideCharToMultiByte(uCodePage, 0, strUCS2, -1, szANSI, size, NULL, NULL) == 0)
  43. {
  44. delete[] szANSI;
  45. return "";
  46. }
  47. CStringA ret = szANSI;
  48. delete[] szANSI;
  49. return ret;
  50. }