MAC地址加减1算法

            如何实现对12位MAC地址的加减1操作,这里有一个小的算法,其直接在字符串格式的MAC上进行加减操作.不用进行类型转换.

例如:

 

input          000000000054

decrease   000000000053

increase    000000000055

 

 

#define BOOL int #define FALSE 0 #define TRUE 1 #include #include BOOL macDecrease(char * mac) //DECREASE MAC BY ONE { int num=11; while(num>=0) { if('A' == mac[num]) { mac[num]='9'; break; } else if('0' == mac[num]) { mac[num]='F'; num--; } else { mac[num]--; break; } } if(-1 == num) return FALSE; //DECREASE FAIL else return TRUE; } BOOL macIncrease(char * mac) //INCREASE MAC BY ONE { int num=11; while(num>=0) { if('9' == mac[num]) { mac[num]='A'; break; } else if('F' == mac[num]) { mac[num]='0'; num--; } else { mac[num]++; break; } } if(-1 == num) return FALSE; //inclease FAIL else return TRUE; } void main() { char mac[13],macInc[13]; printf("please input the mac :/n"); scanf("%s",mac); strcpy(macInc,mac); if(TRUE == macDecrease(mac)) printf("new mac for decrease is :%s/n",mac); else printf("wrong in decrease/n"); if(TRUE == macIncrease(macInc)) printf("new mac for increase is :%s/n",macInc); else printf("wrong in increase/n"); }  

你可能感兴趣的:(MAC地址加减1算法)