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