实际编程中,MAC地址多用整型来表示,如unsigned long long等。整型对于机器是友好的,对于编码人员就不那么友好了,毕竟我们还是喜欢用string类型的MAC地址,如“00-00-00-00-00-00”,下面就写了个简单的小方法,供参考、使用;
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
typedef unsigned long long UNLONG_64;
string macToStr(UNLONG_64& ulMac)
{
string strMac = "00-00-00-00-00-00";
string strMacTmp;
UNLONG_64 ulMacTmp = ulMac;
char chHex[17] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
chHex[16] = '\0';
for (int i = 0; i < 12; i++)
{
strMacTmp = chHex[ulMacTmp & 0x0F] + strMacTmp;
ulMacTmp = ulMacTmp >> 4;
if (0 == (i+1)%2 && i < 10)
{
strMacTmp = '-' + strMacTmp;
}
if (0 == ulMacTmp)
{
break;
}
}
strMac = strMac.replace(17 - strMacTmp.length(), 17, strMacTmp);
return strMac;
}