-3

I am trying to emulate some C++ code in C#. I am not familiar with the intricate workings of C++ and don't quite understand how to implement this code in C#.

Could someone please explain what the functions are doing and what their output would be in ASCII? In particular, I do not understand what the "memcpy" method is doing the way this code is written.

//example values
str = "<Request Type="Query" Version="1.0"></Request>"
uintcrc = getCrc(str, strlen(str));

//code i don't understand

//create a byte array with a null terminator?
memset(strQueryBuffer, '\0', sizeof(str));
//print the values into the byte array
sprintf(strQueryBuffer, "%c%s%c", COMM_STX, str, COMM_ETX);

//append the uintcrc to the end of the byte array?
memcpy(strQueryBuffer + strlen(strQueryBuffer), &uintcrc, sizeof(uintcrc));
4

2 回答 2

4

它除了

 strQueryBuffer = COMM_STX + "<Request Type='Query' Version="1.0"></Request>" + COMM_ETX + Encoding.Ascii.GetString(BitConverter.GetBytes(uintcrc));

如果你有一个二进制系统并且想要发送完整的信息二进制,你也可以写

 var str = "<Request Type='Query' Version="1.0"></Request>";
 byte[] Data = (new [] { COMM_STX }).Concat(Encoding.Ascii.GetBytes(str)).Concat(new [] { COMM_ETX }).Concat(BitConverter.GetBytes(uintcrc)).ToArray();
于 2012-09-24T19:01:40.780 回答
0
strQueryBuffer + strlen(strQueryBuffer) 

&strQueryBuffer[strlen(strQueryBuffer)]

因此它将 crc 的二进制值附加到 strQueryBuffer 的末尾

于 2012-09-24T19:04:19.840 回答