怎么通过C语言自动生成MAC地址
发布网友
发布时间:2022-04-26 09:24
我来回答
共2个回答
热心网友
时间:2022-06-26 17:07
代码如下,仅供参考:
// MAC.c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#define RANDOM(x) (rand()%x)
#define MAC_ADDR_LENGTH 12
#define FMT_MAC_ADDR_LEN (MAC_ADDR_LENGTH+5)
unsigned char HEXCHAR[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C',
'D','E','F'};
unsigned char genMACAddr[MAC_ADDR_LENGTH];
unsigned char fmtMACAddr[FMT_MAC_ADDR_LEN]={'0','0','-','0','0','-','0','0','-',
'0','0','-','0','0','-','0','0'};
void formatMACAddr()
{
unsigned short i=0;
unsigned short n=0;
for(i=0;i<MAC_ADDR_LENGTH;i++,n++)
{
if(fmtMACAddr[n]!='-')
{
fmtMACAddr[n]=genMACAddr[i];
}
else
{
n++;
fmtMACAddr[n]=genMACAddr[i];
}
}
}
void main()
{
unsigned short i=0;
unsigned short n=0;
srand(getpid());
for(int i=0;i<MAC_ADDR_LENGTH;i++)
{
n=RANDOM(16);
genMACAddr[i]=HEXCHAR[n];
}
formatMACAddr();
printf("%s",fmtMACAddr);
}
热心网友
时间:2022-06-26 17:07
随机的IP地址么 不难