C/C++ 常用功能整理
1. 十六进制字符串转数字
#include <iostream>
using namespace std;
int GetPTZParam(const char* str);
int main()
{
char szCMD[] = { "A50F0109898900B5" };
GetPTZParam(szCMD);
return 0;
}
int GetPTZParam(const char* str)
{
const int size_PTZCmd = 16;
unsigned char xml_ptzcmd[8]{};
const char* pCMD = str;
for (size_t i = 0; i < size_PTZCmd; i+=2)
{
char szbuf[size_PTZCmd]{};
szbuf[0] = pCMD[i];
szbuf[1] = pCMD[i+1];
long code = strtol(szbuf, NULL, 16);
xml_ptzcmd[i / 2] = static_cast<unsigned char>(code);
cout << "pCMD[" << i << "] = " << pCMD[i] << pCMD[i+1] <<", 16进制为:"<< long (xml_ptzcmd[i / 2]) << endl;
}
return 0;
}