Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. string myName;
  2. getline( cin , myName );
  3. wstring printerName( L(myName) ); // error C3861: 'L': identifier not found
  4. wchar_t* WprinterName = printerName.c_str(); // error C2440: 'initializing' : cannot convert from 'const wchar_t *' to 'wchar_t *'
  5.  
  6. inline std::wstring convert( const std::string& as )
  7. {
  8. // deal with trivial case of empty string
  9. if( as.empty() ) return std::wstring();
  10.  
  11. // determine required length of new string
  12. size_t reqLength = ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), 0, 0 );
  13.  
  14. // construct new string of required length
  15. std::wstring ret( reqLength, L'' );
  16.  
  17. // convert old string to new string
  18. ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), &ret[0], (int)ret.length() );
  19.  
  20. // return new string ( compiler should optimize this away )
  21. return ret;
  22. }
  23.  
  24. inline std::wstring convert( const std::string& as )
  25. {
  26. wchar_t* buf = new wchar_t[as.size() * 2 + 2];
  27. swprintf( buf, L"%S", as.c_str() );
  28. std::wstring rval = buf;
  29. delete[] buf;
  30. return rval;
  31. }
  32.  
  33. std::string str = "Hello";
  34. std::wstring str2(str.length(), L' '); // Make room for characters
  35.  
  36. // Copy string to wstring.
  37. std::copy(str.begin(), str.end(), str2.begin());
  38.  
  39. wstring printerName;
  40. printerName.assign( myName.begin(), myName.end() );
  41.  
  42. #include <atlbase.h>
  43.  
  44. std::wstring wideString(L"My wide string");
  45. std::string narrowString("My not-so-wide string");
  46.  
  47. ATL::CW2A narrow(wideString.c_str()); // narrow is a narrow string
  48. ATL::CA2W wide(asciiString.c_str()); // wide is a wide string
  49.  
  50. errno_t wcstombs_s(
  51. size_t *pReturnValue,
  52. char *mbstr,
  53. size_t sizeInBytes,
  54. const wchar_t *wcstr,
  55. size_t count
  56. );
  57.  
  58. errno_t mbstowcs_s(
  59. size_t *pReturnValue,
  60. wchar_t *wcstr,
  61. size_t sizeInWords,
  62. const char *mbstr,
  63. size_t count
  64.  
  65. //I think this code is simple enough
  66.  
  67. #include <string>
  68. using std::wstring;
  69. using std::string;
  70.  
  71. wstring Convert_String_To_WideString(string & stringtoconvert)
  72. {
  73. wstring s = L""; // wstring to return
  74. wchar_t b = '0'; // to append new wstring
  75. int size = stringtoconvert.size();
  76.  
  77. // convert here
  78. for(int i = 0; i < size ; i++)
  79. {
  80. b = stringtoconvert[i];
  81. s.append(1,b);
  82. }
  83.  
  84. return s; // return wide string
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement