code_junkie

How do you properly use WideCharToMultiByte

Nov 11th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <wchar.h>
  4. #include <string.h>
  5.  
  6. /* string consisting of several Asian characters */
  7. wchar_t wcsString[] = L"u9580u961cu9640u963fu963bu9644";
  8.  
  9. int main()
  10. {
  11.  
  12. size_t wcsChars = wcslen( wcsString);
  13.  
  14. size_t sizeRequired = WideCharToMultiByte( 950, 0, wcsString, -1,
  15. NULL, 0, NULL, NULL);
  16.  
  17. printf( "Wide chars in wcsString: %un", wcsChars);
  18. printf( "Bytes required for CP950 encoding (excluding NUL terminator): %un",
  19. sizeRequired-1);
  20.  
  21. sizeRequired = WideCharToMultiByte( CP_UTF8, 0, wcsString, -1,
  22. NULL, 0, NULL, NULL);
  23. printf( "Bytes required for UTF8 encoding (excluding NUL terminator): %un",
  24. sizeRequired-1);
  25. }
  26.  
  27. Wide chars in wcsString: 6
  28. Bytes required for CP950 encoding (excluding NUL terminator): 12
  29. Bytes required for UTF8 encoding (excluding NUL terminator): 18
  30.  
  31. #include <string>
  32.  
  33. std::string wstrtostr(const std::wstring &wstr)
  34. {
  35. // Convert a Unicode string to an ASCII string
  36. std::string strTo;
  37. char *szTo = new char[wstr.length() + 1];
  38. szTo[wstr.size()] = '';
  39. WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL);
  40. strTo = szTo;
  41. delete[] szTo;
  42. return strTo;
  43. }
  44.  
  45. std::wstring strtowstr(const std::string &str)
  46. {
  47. // Convert an ASCII string to a Unicode String
  48. std::wstring wstrTo;
  49. wchar_t *wszTo = new wchar_t[str.length() + 1];
  50. wszTo[str.size()] = L'';
  51. MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wszTo, (int)str.length());
  52. wstrTo = wszTo;
  53. delete[] wszTo;
  54. return wstrTo;
  55. }
  56.  
  57. //pX is an out parameter, it fills your variable with 10.
  58. void fillXWith10(int *pX)
  59. {
  60. *pX = 10;
  61. }
  62.  
  63. int main(int argc, char ** argv)
  64. {
  65. int X;
  66. fillXWith10(&X);
  67. return 0;
  68. }
  69.  
  70. // Convert a wide Unicode string to an UTF8 string
  71. std::string utf8_encode(const std::wstring &wstr)
  72. {
  73. int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
  74. std::string strTo( size_needed, 0 );
  75. WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
  76. return strTo;
  77. }
  78.  
  79. // Convert an UTF8 string to a wide Unicode String
  80. std::wstring utf8_decode(const std::string &str)
  81. {
  82. int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
  83. std::wstring wstrTo( size_needed, 0 );
  84. MultiByteToWideChar (CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
  85. return wstrTo;
  86. }
Add Comment
Please, Sign In to add comment