String, CString, char[] and ASCII character representations

Source: Internet
Author: User

The handling of strings is a tricky issue in C + +, and there is no such problem with managed-based platforms like Java and C #.

Let's discuss the two methods of memcpy and strcpy first.

void* memcpy (void *memto, const void *memfrom, size_t size); char* strcpy (char * dest, const char * src);

There are 3 main differences between the two methods:

1. The copied content is different, strcpy can only copy the string, and memcpy can copy any content, such as Char[],int,struct,class.

2. Different methods of copying, strcpy do not need to specify the length to copy, when encountered in the SRC string "" "(null character) to stop copying, it is prone to overflow phenomenon. memcpy, however, avoids such problems by determining the length of the copy according to its third parameter.

3. The use is different, usually with strcpy when copying the string, and when copying other types of data is generally used memcpy.

4. To copy the Soh with an ASCII of 1, in memcpy, if you enter 0 directly, it represents the 0 character.

It is important to note that:

In the case of sending a command to communicate with the device, many times the command will contain null characters, which should be used with caution, because strcpy will terminate the copy action when encountering a null character, resulting in a command that is not duplicated after the null character is strcpy.

Formatted output of string and CString:

CString strtemp_1, Strname;int nAge = 10;strname = "Xiaoming"; Strtemp.format ("%s%d Years", StrName, NAge);//-------------------- ------------------------------------#include <string> #include <sstream> using namespace std; Ostringstream Ostr; String strtemp_1, strtemp_2; strtemp_1 = "Xiao Ming"; strtemp_2 = "The weather is fine today"; int nAge = 10; Ostr << strtemp_2 << "," << strtemp_1 << "This year" << NAge << "years.  "; String strdest = Ostr.str ();

Note: The next time you use OSTR, it will be added to the previously formatted string, so it needs to be emptied and reused.

String, CString, empty character null

It is important to note that both string and CString do not support null character input, that is, if the string and CString types detect null characters in the strings, empty characters are automatically stripped and the strings are scaled down. So, to store empty characters, you would use a char array, for example:

Char chname[10];chname[2] = 0;  null character chname[3] = ' 0 '; Character 0

String, CString, char[] and ASCII character representations

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.