Differences between string, cstring, and char * and char, wchar_t, and tchar

Source: Internet
Author: User

The relationship and conversion between string, cstring, and char * have always been understandable, and the MFC program does not know when there will be one .. So I sorted out the online things based on my understanding.

I. Overview

FirstChar * (this is the basis)Because it should be the simplest and most basic, it is equivalent to a general int * or double * in terms of function, this basic structure. However, due to the special nature of the string, we use other classes for processing. The string class is used in C ++, And the cstring class is used in MFC. Therefore, basically, char * is the most basic, and string and cstring are only an extension of char.

Ii. Comparison

What are the three relationships? Because string and cstring are both extensions of char *, we can know that both of them should be compatible with char *. Such compatibility can be achieved through constructor and operator =. At the same time, if the advanced string and cstring must be converted to the low-level char *, a function is required. You can use the format function. The following details:

1: string and cstring between the same level

A: string to cstring

Cstring. Format ("% s", String. c_str ());

B: cstring to string

String S (cstring. getbuffer ());

Or

Cstring CSTR; string S = sctr;

Analysis: In a, the string is converted to the char type through the c_str function, and then to the ctring type through the char type.

Getbuffer in B is an internal function of cstring, which returns the pointer to the Memory Character Buffer, which is equivalent to conversion through char.

C is converted by the value assignment operator.

2: low-level to advanced conversion (char * to string or cstring)

A: string to char *

As mentioned above, the c_str function of string implements this conversion, that is, String. c_str () can be used ().

B: cstring to char *

As mentioned above, the getbuffer function can implement this conversion. You can use ctring. getbuffer (), or use lpctstr for forced conversion.

Iii. Summary

Here we can see that the intermediate process is still the basic char *. Remember the two functions string and cstring. c_str () and cstring. getbuffer () can be converted to char * or forced conversion of lpctstr.

Iv. Differences between the attached char, wchar_t, and tchar

There are two encoding methods: ANSI and Unicode. One is American and the other is universal ). Therefore, char corresponds to ANSI, wchar_t corresponds to Unicode, and tchar is both compatible. The header files of the string processing functions corresponding to the three are STR ***, WCS ***, and _ TCS ***.

Take the strcpy function as an example to summarize:

If you want to use an ANSI string, use this method:

Char szstring [100]; strcpy (szstring, "test ");

. If you want to use a unicode string, use this set:

Wchar_t szstring [100]; wcscpyszstring, l "test ");.

If you want to compile ANSI or Unicode string code by defining _ Unicode macro:

Tchar szstring [100]; _ tcscpy (szstring, _ text ("test "));

Note: (most of the MFC systems require generic wchar_t, whereas C ++ generally defines ANSI .)

The conversion between them can be done through the function wcstombs (char * strdes, const wchar *, size_t Nmax); this function is used to convert wchar to Char. Char *
Strdes is the string that saves the converted string, and wchar * is the width string to be converted. The maximum length of the conversion. The length here is the number of conversions, not the byte length. Mbstowcs () is the opposite process.


Reference: http://blog.csdn.net/yanonsoftware/article/details/544428

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.