TrueCrypt代码 之 编码转换

[cpp]  view plain  copy
  1. // Convert.h  
  2. #pragma once  
  3.   
  4. LPSTR  MYW2UTF(LPCWSTR _W, LPSTR _A);  
  5.   
  6. LPWSTR MYUTF2W(LPCSTR _A, LPWSTR _W);  
  7.   
  8. LPSTR  MYW2A(LPCWSTR _W, LPSTR _A);  
  9.   
  10. LPWSTR MYA2W(LPCSTR _A, LPWSTR _W);  
  11.   
  12. LPSTR  MYT2A(LPCTSTR  _T, LPSTR _A);  
  13.   
  14. LPTSTR MYA2T(LPCSTR _A, LPTSTR  _T);  
  15.   
  16. LPWSTR MYT2W(LPCTSTR  _T, LPWSTR _W);  
  17.   
  18. LPTSTR MYW2T(LPCWSTR _W, LPTSTR  _T);  


[cpp]  view plain  copy
  1. //Convert.cpp  
  2. #include "stdafx.h"  
  3. #include "Convert.h"  
  4.   
  5. LPSTR  MYW2A(LPCWSTR _W, LPSTR _A)  
  6. {  
  7.     int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, _W, wcslen(_W), NULL, 0, 0, 0);  
  8.     if (nOutputStrLen != 0)  
  9.     {  
  10.         nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, _W, wcslen(_W), _A, nOutputStrLen, 0, 0);  
  11.     }  
  12.     if (nOutputStrLen > 0)  
  13.     {  
  14.         _A[nOutputStrLen] = '\0';  
  15.     }  
  16.     else  
  17.     {  
  18.         _A[0] = '\0';  
  19.     }  
  20.     return _A;  
  21. }  
  22.   
  23. LPWSTR MYA2W(LPCSTR _A, LPWSTR _W)  
  24. {  
  25.     int nOutputStrLen = MultiByteToWideChar(CP_ACP, 0, _A, strlen(_A), NULL, 0);  
  26.     if (nOutputStrLen != 0)  
  27.     {  
  28.         nOutputStrLen = MultiByteToWideChar(CP_ACP, 0, _A, strlen(_A), _W, nOutputStrLen);  
  29.     }  
  30.     if (nOutputStrLen > 0)  
  31.     {  
  32.         _W[nOutputStrLen] = '\0';  
  33.     }  
  34.     else  
  35.     {  
  36.         _W[0] = '\0';  
  37.     }  
  38.     return _W;  
  39. }  
  40.   
  41. LPSTR  MYW2UTF(LPCWSTR _W, LPSTR _A)  
  42. {  
  43.     int nOutputStrLen = WideCharToMultiByte(CP_UTF8, 0, _W, wcslen(_W), NULL, 0, 0, 0);  
  44.     if (nOutputStrLen != 0)  
  45.     {  
  46.         nOutputStrLen = WideCharToMultiByte(CP_UTF8, 0, _W, wcslen(_W), _A, nOutputStrLen, 0, 0);  
  47.     }  
  48.     if (nOutputStrLen > 0)  
  49.     {  
  50.         _A[nOutputStrLen] = '\0';  
  51.     }  
  52.     else  
  53.     {  
  54.         _A[0] = '\0';  
  55.     }  
  56.     return _A;  
  57. }  
  58.   
  59. LPWSTR MYUTF2W(LPCSTR _A, LPWSTR _W)  
  60. {  
  61.     int nOutputStrLen = MultiByteToWideChar(CP_UTF8, 0, _A, strlen(_A), NULL, 0);  
  62.     if (nOutputStrLen != 0)  
  63.     {  
  64.         nOutputStrLen = MultiByteToWideChar(CP_UTF8, 0, _A, strlen(_A), _W, nOutputStrLen);  
  65.     }  
  66.     if (nOutputStrLen > 0)  
  67.     {  
  68.         _W[nOutputStrLen] = '\0';  
  69.     }  
  70.     else  
  71.     {  
  72.         _W[0] = '\0';  
  73.     }  
  74.     return _W;  
  75. }  
  76.   
  77. LPSTR  MYT2A(LPCTSTR  _T, LPSTR _A)  
  78. {  
  79. #if defined(_UNICODE)  
  80.     return MYW2A(_T, _A);  
  81. #else  
  82.     strcpy(_A, _T);  
  83.     return _A;  
  84. #endif  
  85. }  
  86.   
  87. LPTSTR MYA2T(LPCSTR _A, LPTSTR  _T)  
  88. {  
  89. #if defined(_UNICODE)  
  90.     return MYA2W(_A, _T);  
  91. #else  
  92.     strcpy(_T, _A);  
  93.     return _A;  
  94. #endif  
  95. }  
  96.   
  97. LPWSTR MYT2W(LPCTSTR  _T, LPWSTR _W)  
  98. {  
  99. #if defined(_UNICODE)  
  100.     wcscpy(_W, _T);  
  101.     return _W;  
  102. #else  
  103.     return MYA2W(_T, _W);  
  104. #endif  
  105. }  
  106.   
  107. LPTSTR MYW2T(LPCWSTR _W, LPTSTR  _T)  
  108. {  
  109. #if defined(_UNICODE)  
  110.     wcscpy(_T, _W);  
  111.     return _T;  
  112. #else  
  113.     return MYW2A(_W, _T);  
  114. #endif  
  115. }  

猜你喜欢

转载自blog.csdn.net/bruce135lee/article/details/80528355