When transmitting data with the signature, using snprintf formatted data, where the signature data is sometimes found to have missed
as follows:
char AuthBidirection[320] = { '\0' }; snprintf(AuthBidirection, 320, "Bidirection algorithm=\"%s\",random1=\"%s\",random2=\"%s\",deviceid=\"%s\",serverid=\"%s\",sign1=\"%s\"
", strAlgorithm.c_str(), m_strRandom1.c_str(), strRandom2.c_str(), m_strGbID.c_str(), m_strGbServerID.c_str(), sign1.c_str());
Read:
char AuthBidirection[320] = { '\0' }; snprintf(AuthBidirection, 320, "Bidirection algorithm=\"%s\",random1=\"%s\",random2=\"%s\",deviceid=\"%s\",serverid=\"%s\",sign1=\"", strAlgorithm.c_str(), m_strRandom1.c_str(), strRandom2.c_str(), m_strGbID.c_str(), m_strGbServerID.c_str()); //added sign1 memcpy(AuthBidirection + strlen(AuthBidirection), sign1.c_str(), sign1.size()); //added " end strcat(AuthBidirection,"\"");
Alternative use memcpy
The reason: After the signed data, type std :: sign into the string of data may encounter character \ 0,
Then, then, then. . . , Sign1.c_str () returns the starting address of a character array, which put into sprintf naturally when the \ 0 is over, the back of the data is directly missing. . . . . .
Carry out alone to test:
char *buf = new char[5]; memcpy(buf,(void *)"abc\0d", 5); string strSig; strSig.assign(buf, 5); int len = strSig.size(); std::cout << strSig.size() << std::endl; //5 std::cout << strSig[4] << std::endl; //d char AuthBidirection[320] = { '\0' }; the snprintf (AuthBidirection, 320. , " strSig =% S " , strSig.c_str ()); STD :: COUT << AuthBidirection [ . 11 ] << STD :: endl; // print null characters, because no go into, both behind It is empty char AuthBidirection1[320] = { '\0' }; snprintf(AuthBidirection1, 320, "strSig=%s", ""); memcpy(AuthBidirection1 + strlen(AuthBidirection1), strSig.c_str(),len); std::cout << AuthBidirection1[11] << std::endl; //d
Output:
5
d'
d
The final went into a d, a no
Thus, when using string data processing, the size of the size needs special attention, in particular, relates to a network when the contract, pay special attention to the use of strSig.c_str () to process, strSig.c_str () is only a start address,
Thus encountered string manipulation functions, such as snprintf, etc., are to \ numerals 0 to end processing, it is necessary to carefully again. It had also experienced similar, so in order to quickly identify problems and record it. . . .