Conversion between VC char and tchar

Source: Internet
Author: User

Char: A Basic data type that can contain a single character in a computer programming language (C, C ++, Java, VFP, and so on.

Tchar: To meet unicode encoding, the extension of char, that is, _ T ("str") indicates the tchar type

C ++ supports two types of strings: Regular ANSI encoding (using "" packages) and Unicode encoding (using L "" packages ), in this way, there are two sets of string processing functions, such as strlen and wcslen, which are used to process two types of string char and tchar respectively.

In the WINNT. h header file:

Typedef wchar tchar, * ptchar;

Indicates that tchar and wchar belong to the same type


Char sza [100]; // ANSI String Buffer

Wchar SZW [100]; // Unicode String Buffer

// Normal sprintf: All strings are ANSI
Sprintf (sza ,"% S"," ANSI Str ");

// Converts Unicode string to ANSI
Sprintf (sza ,"% S", L" Unicode Str ");

// Normal swprintf: All strings are Unicode
Swprintf (SZW, l"% S", L" Unicode Str ");

// Converts ANSI string to Unicode

Swprintf (SZW, l"% S"," ANSI Str ");


Note:Use of uppercase and lowercase s

======================================


Application instance:Start the MSC program through the system function program call

Void WSUS: onbnclickedok () {cstring strpath = NULL; // request path string (tchar) Char strchar [256]; // request path string (char) m_medmedit.getwindowtextw (strpath ); // obtain the path and store it in strpathstrpath. replace (_ T ("\"), _ T ("\"); // Replace "\" with "\" in strpath "\\", note the conversion character // sprintf (strchar, "% S % s", "mmc.exe", strpath); // tchar converts the char type sprintf (strchar, "mmc.exe \" % s \ "", strpath); // tchar conversion char type MessageBox (strpath, _ T ("title"); System (strchar ); // start the MSC program by calling the system function // winexec (lpcstr) _ bstr_t (strpath), sw_show); // call the EXE program}

Example steps:

1. Obtain the MSC program path strpath

2. Replace the character "\" in strpath "\"

C: \ windows \ system32 \ gpedit. MSC

First, convert it:

C: \ Windows \ system32 \ gpedit. MSC

Then, use sprintf (strchar, "% S % s", "mmc.exe", strpath); concatenate the string as follows:

Mmc.exe C :\\ windows \ system32 \ gpedit. MSC

3. Start the MSC program by calling the system function
System (strchar );

4. the startup result is as follows:

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.