最近,项目中使用了LM3S8962这个芯片,来让投影机具有网络控制功能,这个模块主要就是内部有一个简单的WEB服务器,可以进行SSI和CGI的编程,其还有串口,通过和DDP内部的UART口相连,两者可以相互通信,从而达到网络控制投影机的功能.但是,每一个网络模块都需要一个MAC地址,怎么办呢?
然后,我自己用C语言写了一个随机生成MAC地址的,程序如下:
#include
#include
#include
#include
#define DBG_PROGRAM 0
#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(void)
{
unsigned short i = 0;
unsigned short n = 0;
for( i = 0; i < MAC_ADDR_LENGTH; i++, n++ )
{
if( fmtMACAddr[n] != '-' )
{
fmtMACAddr[n] = genMACAddr;
}
else
{
n++;
fmtMACAddr[n] = genMACAddr;
}
}
}
int main(int argc, char* argv[])
{
unsigned short n = 0;
unsigned short i = 0;
unsigned long j = 0;
unsigned long num = 0;
FILE *fp = 0;
if( argc != 3 )
{
printf("Usage: generate_mac_addr savefile.txt 1000");
return -1;
}
num = atol( argv[2] );
fp = fopen( argv[1], "w" );
if( !fp )
{
printf("create the file %s failed\r\n",argv[1]);
return -1;
}
fprintf( fp, "NO.\t\tMAC Address 1\t\tMAC Address 2\r\n" );
srand(getpid());
for( j = 0; j < num; j++ )
{
for( i = 0; i < MAC_ADDR_LENGTH; i++ )
{
n = RANDOM(16);
#if DBG_PROGRAM
printf("Times: %d, Random Number: %d, HEX char: %c\r\n",i,n,HEXCHAR[n]);
#endif
genMACAddr = HEXCHAR[n];
}
#if DBG_PROGRAM
printf("writing the MAC address......%ld\r\n",(j+1));
#endif
formatMACAddr();
fprintf( fp, "%ld\t\t%s\t\t%s\r\n", (j+1), genMACAddr, fmtMACAddr );
}
fclose(fp);
printf("generate the MAC address successfully!\r\n");
return 0;
}
我使用的是CodeBlocks10.05的GCC编译器进行编译的,编译之后,程序大小只有6.5KB,而我用它生成100000个MAC地址,并存储到一个txt文件中,生成的文件的大小为3994KB,用时居然不到1秒,看来C语言真的快呀,呵呵~
编译之后,会生成exe文件,使用格式:generate_mac_addr MACList1000.txt 100000