📄 mac_exchange.c
字号:
//link_mac() and better_link_mac()能把12:12:12:12:12:12转换成1212121212
//而div_mac()能把121212121212转换成12:12:12:12:12:12的形式
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define Mac_Leng_L 17
#define Mac_Leng_S 12
#define Mac_Point_C 5
#define Mac_Size 18
//the bug: if the tp is 0::0:99:5:66,it will get the wrong mac
int link_mac(char *dst,char * tp)
{
char tmp[Mac_Size];
int i;
for(i=0;*tp!='\0';tp++)
{
if(*tp==':')
continue;
else
*(tmp+i)=*tp;
i++;
}
*(tmp+i)='\0';
strcpy(dst,tmp);
return 0;
}
//it can change the standard long mac addr to standard mac short addr
//--12:12:12:12:12:12 to 121212121212
int better_link_mac(char *dst,char *tp)
{
char tmp[Mac_Size];
int count=0;
int i;
if((tp==NULL)||(strlen(tp)!=Mac_Leng_L))
{
strcpy(dst,"");
return -1;
}
for(i=0;*tp!='\0';tp++)
if(*tp==':')
count+=1;
else{
*(tmp+i)=*tp;
i++;
}
*(tmp+i+1)='\0';
if(count!=Mac_Point_C)
{
strcpy(dst,"");
return -1;
}
else
strcpy(dst,tmp);
return 0;
}
//change the 1212121212 to 12:12:12:12:12:12
int div_mac(char * dst,char * tp)
{
char tmp[Mac_Size];
int i=1;
if((tp==NULL)||(strlen(tp)!=Mac_Leng_S))
return -1;
for(;i<Mac_Leng_L||*tp!='\0';i++)
if(i%3==0)
tmp[i]=':';
else
tmp[i]=*tp++;
tmp[i]='\0';
strcpy(dst,tmp);
return 0;
}
int main(){
int i;
char mac1[Mac_Size]="10:00:00:00:00:01";
char mac2[Mac_Size]="200000000002";
char mac3[Mac_Size];
i=div_mac(mac3,mac2);
if(i==-1)
return -1;
printf("the mac is %s\n",mac3);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -