</pre><pre>
Unicode, ANSI strings are often encountered in windows/mfc,c++ programming, and these strings need to be converted, and the system and its transformations are summarized in this paper.
First article: Basic article
3 common functions and analysis of large system See blog: http://blog.csdn.net/u010003835/article/details/47344775
In addition, the next function I write is to write the naming rules for Windows on Char*,tchar*,const tchar*
Unfamiliar children's shoes can refer to the blog: http://blog.csdn.net/u010003835/article/details/47604553
TCHAR Type , select the character set according to the environmental Judgment
Required String Conversion Macros:
_t ()/TEXT () converts a string based on the current environment
The difference between it and L See blog: http://blog.csdn.net/u010003835/article/details/47606621
Examples of representative functions and their proper use:
LPTSTR temp_t = new TCHAR[MAXN];
Temp_t[0] = 0;
_tcscpy (temp_t, _t ("Hi, kemeng~"));
_tprintf (_t ("_tprintf:%s\n"), temp_t);
Delete[] temp_t;
WCHAR Type, using Unicode encoding
Required string Conversion macro: L "XXXX" To encode the current string in Unicode format
Test code:
LPWSTR temp_w = new WCHAR[MAXN];
Temp_w[0] = 0;
wcscpy (Temp_w, L "Hi, kemeng~");
wprintf (L "wprintf:%s\n", temp_w);
Delete[] Temp_w;
Common errors:
The first type:
wprintf ("%s", L "aaaaaa");
Another type of:
wprintf (L "%s", "aaaaaa");
Correct wording:
wprintf (L "%s", L "aaaaaa");
Wcout seems more intelligent, will convert the original string, estimated by overloading, after testing in the supplementary
ASCII encoding,
String conversion macros are not required
Sample code
LPSTR Temp_c = new Char[maxn];temp_c[0] = 0;strcpy (Temp_c, "Hi, kemeng~");p rintf ("printf:%s\n", Temp_c);d elete[] Temp_c;
Chapter II: Advanced article
Wchar,tchar,char conversions between types of strings:
Method One: Use macros such as A2T.
The name of the macro means see the blog post:
http://blog.csdn.net/u010003835/article/details/47609975
If you use these macros you need to include the header file:
#include <atlbase.h>
and to declare
Uses_conversion; //T2A,A2T,W2T,W2A,A2CW and other support, need to make this statement
The Complete test code:
#include <iostream> #include <windows.h> #include <atlbase.h> #include <tchar.h>using namespace std; #define MAXN 255#pragma Warning (disable:4996) int main () {USES_CONVERSION;//T2A,A2T,W2T,W2A,A2CW etc support, This declaration is required//T2A. TCHAR the conversion of a string to a char string. cout << Endl << "TCHAR convertion to char use T2A" << Endl; LPTSTR name1_t = _t ("Hi, kemeng~"); LPSTR Temp1_c = Null;temp1_c = T2A (name1_t), _tprintf (_t ("%s\n"), name1_t);p rintf ("%s\n", Temp1_c), cout << "------ --------------------"<< endl;//a2t. Char string to TCHAR string conversion cout << Endl << "char convertion to TCHAR use a2t" << Endl; LPSTR Name2_c = "Hi, kemeng~"; LPTSTR temp2_t = a2t (name2_c);p rintf ("%s\n", Name2_c), _tprintf (_t ("%s\n"), temp2_t) cout << "------------------ --------"<< endl;//w2t. WCHAR string to TCHAR string conversion cout << Endl << "WCHAR convertion to TCHAR use w2t" << Endl; LPWSTR name3_w = L "Hi, kemeng~"; LPTSTR temp3_t = t2w (name3_w); wprintf (L "%s\n", name3_w); _tprintf(_t ("%s\n"), temp3_t) cout << "--------------------------" << endl;//w2t. WCHAR string to const char string conversion cout << Endl << "WCHAR convertion to const char use W2CA" << Endl; LPWSTR name4_w = L "Hi, kemeng~"; LPCSTR temp4_a = W2CA (Name4_w), wprintf (L "%s\n", Name4_w);p rintf ("%s\n", temp4_a); cout << "--------------------- -----"<< Endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
TCHAR System/char System/wchar system and its mutual transformation in windows/mfc,c++