"Programming Supplements" VC + + LPCTSTR, CString, char *, string conversion between

Source: Internet
Author: User

Character

In VC2012, the character set defaults to the Unicode character set (use Unicode charecter set option), and its value can also be set to a multibyte character set (using Multi-Byte Charecter set).

Why do you use Unicode
  • It is easy to exchange data between different languages. The
  • enables you to assign a single binary. exe file or DLL file that supports all languages.
  • Windows 2000 was developed from scratch using Unicode, and if you call any of the Windows functions and pass an ANSI string to it, the system first converts the string to Unicode and then passes the Unicode string to the operating system. If you want the function to return an ANSI string, the system first converts the Unicode string to an ANSI string, and then returns the result to your application. The conversion of these strings takes up the time and memory of the system. By developing an application from scratch with Unicode, you can make your application run more efficiently.
    When Microsoft Corporation converted COM from 16-bit Windows to Win32, the company decided that all COM interface methods that needed strings would accept only Unicode strings.

    What are the Unicode data types defined by Windows

    WCHAR Unicode characters;
    Pwstr pointer to a Unicode string;
    Pcwstr pointer to a constant Unicode string;
    The corresponding ANSI data types are CHAR,LPSTR and LPCSTR;
    The Ansi/unicode universal data type is TCHAR,PTSTR,LPCTSTR.

    How to work with Unicode

    ANSI operation function starts with STR strcpy
    The Unicode action function starts with WCS wcscpy
    The MBCS action function starts with _mbs _mbscpy
    Ansi/unicode action function starts with _tcs _tcscpy (C run-time library)
    Ansi/unicode action function starts with LSTR lstrcpy (Windows functions)
    All new and obsolete functions have both ANSI and Unicode two versions in Windows2000. The end of the ANSI version function is represented by a, and the Unicode version function ends with W. Windows is defined as follows:

    #ifdef UNICODE #define CREATEWINDOWEX CREATEWINDOWEXW #else #define CREATEWINDOWEX createwindowexa #endif//! Unicode
    How to represent Unicode string constants

    ANSI "string"
    Unicode L "string"
    Ansi/unicode T ("string") or _text ("string") if (szerror[0] = = _text (' J ')) {}

    About _t and L

    _t will automatically convert Unicode and non-unicode,l to Unicode based on your project's settings.
    Visual C + + in the definition of strings, with _t to ensure compatibility, is a data type, but it does not produce results, is compiled by the system's preprocessing system to explain, VC support ASCII and Unicode two character types, with _ T guarantees that the program does not need to be modified when converting from an ASCII encoding type to a Unicode encoding type.

    _t is a macro definition that converts a string to Tchar,tchar, and Tchar equals WCHAR when Unicode is defined, otherwise it is equivalent to char. For compatibility with future platforms, it is recommended to use TCHAR instead of ordinary char. Example: TCHAR s = _t ("Fsdf"). L Convert strings to WCHAR for environments that require Unicode. Example: WCHAR s = L "Fsdf".

    LPCTSTR and CString's relationship LPCTSTR type:
    l represents a long pointer this is for compatibility with 16-bit operating systems such as Windows 3.1, in Win32 and other 32-bit operating systems, the long pointer and the near pointer and the far modifier are all for compatibility purposes. No practical significance.
    P indicates that this is a pointer
    C means a constant
    T means that there is a _t macro in the WIN32 environment
    STR indicates that the variable is a string

    This macro is used to indicate whether your character uses Unicode, and if your program defines Unicode or other related macros, then the character or string will be used as a Unicode string, otherwise the standard ANSI string.
    So LPCTSTR represents a string that points to a constant fixed address that can change semantics based on some macro definitions.
    We use a type definition with T most of the time in the program.
    LPCTSTR is equivalent to const TCHAR *, which was previously seen TCHAR as Ansi/unicode universal data type, when Unicode was defined TCHAR equals WCHAR, otherwise it is equivalent to char.

    The distinction between a constant string ANSI and Unicode is determined by the macro _t. However, with _t ("ABCD"), the string "ABCD" will determine whether Char or W_charis based on the negation of _unicode at compile time. Similarly, TCHAR is the same as the target Word macros.
    In the case of ANSI, LPCTSTR is a const char, a constant string (which cannot be modified), and LPTSTR is a char , an ordinary string (very large, modifiable).

    CString and LPCTSTR

    cstring and LPCTSTR can be said to be universal. The reason for this is the automatic type conversion defined by CString, nothing fancy, the simplest C + + operator overloading. Both of the
    CString and LPCTSTR are basic types, and CString is a C + + class, which is the minimum task to be compatible with these two basic types.
    When you need a const char   and pass in CString, the C + + compiler automatically calls the CString overloaded operator LPCTSTR () for implicit type conversions.
    The C + + compiler automatically calls CString's constructor to construct a temporary CString object when CString is required and a const CHAR&NBSP is passed in, and
    (in fact, char * can).

    CString Turn LPCTSTR:
    CString cStr;
    const char *lpctstr= (LPCTSTR) cStr;
    LPCTSTR Turn CString:
    LPCTSTR LPCTSTR;
    CString Cstr=lpctstr;

    How to convert a string between Unicode and ANSI

    The Windows function MultiByteToWideChar is used to convert a multibyte string into a wide string; the function WideCharToMultiByte converts a wide string into an equivalent multibyte string.

    CString-->char *
    CString str1 =_t ("123"); int len = WideCharToMultiByte (cp_acp,0,str1,-1,null,0,null,null); char *ptxttemp = new Char[len + 1]; WideCharToMultiByte (Cp_acp,0,str1,-1,ptxttemp,len,null,null);//...delete[] ptxttemp;
    char *-->cstring
     
    char *p = "Test  "; CString str (p); 
     
    char * Pfilename = "Hello, world!" Hello,world ";//Calculate char * array size, in bytes, a kanji account of two bytes int charlen = strlen (pfilename);//calculate the size of multibyte characters, by character. int len = MultiByteToWideChar (cp_acp,0,pfilename,charlen,null,0);//Request space for wide-byte character array, array size is multibyte character size computed by byte tchar *buf = new Tchar[len + 1];//multibyte encoding converted to wide-byte encoding MultiByteToWideChar (Cp_acp,0,pfilename,charlen,buf,len); Buf[len] = ' n '; Add the end of the string, note that it is not len+1//to convert the TCHAR array to cstringcstring pwidechar;pwidechar.append (BUF);//delete buffer delete []buf; 

    Reprint please indicate the author Jason Ding and its provenance
    GitHub Blog Home page (http://jasonding1354.github.io/)
    CSDN Blog (http://blog.csdn.net/jasonding1354)
    Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)

    "Programming Supplements" VC + + LPCTSTR, CString, char *, string conversion between

    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.