Wchar_t *, wchar_t, wchat_t array, Char, char *, char array, STD: String, STD: wstring, cstring .... PK

Source: Internet
Author: User

  1. # Include <string>
  2. // You must use MFC to use cstring and cannot contain <windows. h>
  3. # DEFINE _ afxdll
  4. # Include <afx. h>
  5. Using namespace STD;
  6. //----------------------------------------------------------------------------------
  7. // Convert single-byte char * to wide-byte wchar *
  8. Inline wchar_t * ansitounicode (const char * szstr)
  9. {
  10. Int nlen = multibytetowidechar (cp_acp, mb_precomposed, szstr,-1, null, 0 );
  11. If (nlen = 0)
  12. {
  13. Return NULL;
  14. }
  15. Wchar_t * presult = new wchar_t [nlen];
  16. Multibytetowidechar (cp_acp, mb_precomposed, szstr,-1, presult, nlen );
  17. Return presult;
  18. }
  19. //----------------------------------------------------------------------------------
  20. // Convert the wide byte wchar_t * to a single byte char *
  21. Inline char * unicodetoansi (const wchar_t * szstr)
  22. {
  23. Int nlen = widechartomultibyte (cp_acp, 0, szstr,-1, null, 0, null, null );
  24. If (nlen = 0)
  25. {
  26. Return NULL;
  27. }
  28. Char * presult = new char [nlen];
  29. Widechartomultibyte (cp_acp, 0, szstr,-1, presult, nlen, null, null );
  30. Return presult;
  31. }
  32. //----------------------------------------------------------------------------------
  33. // Convert a single-character string to a wide-character wstring
  34. Inline void ascii2widestring (const STD: string & szstr, STD: wstring & wszstr)
  35. {
  36. Int nlength = multibytetowidechar (cp_acp, 0, szstr. c_str (),-1, null, null );
  37. Wszstr. Resize (nlength );
  38. Lpwstr lpwszstr = new wchar_t [nlength];
  39. Multibytetowidechar (cp_acp, 0, szstr. c_str (),-1, lpwszstr, nlength );
  40. Wszstr = lpwszstr;
  41. Delete [] lpwszstr;
  42. }
  43. //----------------------------------------------------------------------------------
  44. Int _ tmain (INT argc, _ tchar * argv [])
  45. {
  46. Char * pchar = "I like char ";
  47. Wchar_t * pwidechar = l "I hate wchar_t ";
  48. Wchar_t tagwidecharlist [100];
  49. Char CH = 'a ';
  50. Char tagchar [100] = {null };
  51. Cstring CSTR;
  52. STD: String STR;
  53. // Note: Set the language environment to output widechar
  54. Setlocale (lc_all, "CHS ");
  55. // Note: char * converts wchar_t *
  56. // Note: wchar_t is not overloaded <, so cout cannot be used <output
  57. Pwidechar = ansitounicode (pchar );
  58. // Note: printf ("% ls") and wprintf (L "% s") are consistent
  59. Printf ("% ls/N", pwidechar );
  60. // Note: wchar_t * converts wchar_t []
  61. Wcscpy (tagwidecharlist, pwidechar );
  62. Wprintf (L "% s/n", tagwidecharlist );
  63. // Note: wchar_t [] converts wchar_t *
  64. Pwidechar = tagwidecharlist;
  65. Wprintf (L "% s/n", pwidechar );
  66. // Note: Char converts string
  67. Str. insert (Str. Begin (), ch );
  68. Cout <STR <Endl;
  69. // Note: wchar_t * converts string
  70. Pwidechar = new wchar_t [Str. Length ()];
  71. Swprintf (pwidechar, l "% s", str. c_str ());
  72. Wprintf (L "% s/n", pwidechar );
  73. // Note: String Conversion char *
  74. Pchar = const_cast <char *> (Str. c_str ());
  75. Cout <pchar <Endl;
  76. // Note: char * converts string
  77. STR = STD: string (pchar );
  78. // Note: cout <reloads the string. If printf is used, printf ("% s", str. c_str () is required ());
  79. // But cannot print ("% s", STR); Because STR is a string class
  80. Cout <STR <Endl;
  81. // Note: String Conversion char []
  82. STR = "Bored ";
  83. Strcpy (tagchar, str. c_str ());
  84. Printf ("% s/n", tagchar );
  85. // Note: String Conversion cstring;
  86. CSTR = Str. c_str ();
  87. // Note: cstring conversion string
  88. STR = string (CSTR. getbuffer (CSTR. getlength ()));
  89. // Note: char * converts cstring
  90. CSTR = pchar;
  91. // Note: cstring converts char *
  92. Pchar = CSTR. getbuffer (CSTR. getlength ());
  93. // Note: cstring converts char []
  94. Strncpy (tagchar, (lpctstr) cstring, sizeof (tagchar ));
  95. // Note: cstring conversion wchar_t *
  96. Pwidechar = CSTR. allocsysstring ();
  97. Printf ("% ls/N", pwidechar );
  98. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.