废话少说,直接上代码:
std::string MacToStr(UINT8 * pMac,const char chKen/* = ':'*/)
{
char szFormat[] = "%02X:%02X:%02X:%02X:%02X:%02X";
int nLen = strlen(szFormat);
if (chKen != ':')
{
for (int i = 2;i < nLen;i += 3)
{
szFormat[i] = chKen;
}
}
char szMac[32] = {0};
sprintf(szMac, szFormat, pMac[0], pMac[1], pMac[2], pMac[3], pMac[4], pMac[5]);
std::string strRet(szMac);
return strRet;
}
UINT8 charToData(const char ch)
{
switch(ch)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a':
case 'A': return 10;
case 'b':
case 'B': return 11;
case 'c':
case 'C': return 12;
case 'd':
case 'D': return 13;
case 'e':
case 'E': return 14;
case 'f':
case 'F': return 15;
}
return 0;
}
bool StrToMac(const char * szMac,UINT8 * pMac)
{
const char * pTemp = szMac;
for (int i = 0;i < 6;++i)
{
pMac[i] = charToData(*pTemp++) * 16;
pMac[i] += charToData(*pTemp++);
pTemp++;
}
}