//************************************ // Method: strToHex 十六进制字符串转16进制 // FullName: CommUtil::strToHex // Access: public // Returns: int XOR异或值 // Qualifier: // Parameter: char * hexStr 十六进制字符串 // Parameter: unsigned char * outData 存储16进制容器 //************************************ int strToHex(char* hexStr, unsigned char *outData) { char *tmpStr = new char[strlen(hexStr)]; strcpy(tmpStr, hexStr); char* res = strtok(tmpStr, " "); int tmpXor = 0x00; int i = 0; while (res != NULL) { int r = strtoull(res, NULL, 16); outData[i] = r; tmpXor ^= r; //std::cout << res << " " << r << " outData:" << ((int)outData[i]) << std::endl; i++; res = strtok(NULL, " "); } return tmpXor; }
调用:
int main() { unsigned char data[3]; strToHex("AA BB CC", data); cout << data; }