#include<stdio.h> #define MAC_LEN 6 int is_null_mac(unsigned int *mac) { unsigned int null_mac[MAC_LEN] = {0, 0, 0, 0, 0, 0}; return (!(mac && (memcmp(mac, null_mac, sizeof(null_mac)) != 0))); } int main() { int ret = 0; //char *get_mac = "01005E010106"; char *get_mac = "000000000000"; unsigned int mac_addr[MAC_LEN] = {0}; char final[18] = "0"; sscanf(get_mac, "%02X%02X%02X%02X%02X%02X \t", &mac_addr[0],&mac_addr[1],&mac_addr[2],&mac_addr[3],&mac_addr[4],&mac_addr[5]); ret = is_null_mac(mac_addr); snprintf(final, sizeof(final),"%02X:%02X:%02X:%02X:%02X:%02X \t", mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]); if (ret == 1) { printf("Mac:[%s] is NULL \n", final); } else { printf("Mac:[%s] is not NULL \n", final); } return 1; }
Output:
Mac:[00:00:00:00:00:00] is NULL
|
Program for valid MAC
#include<stdio.h> #define MAC_LEN 6 int is_null_mac(unsigned int *mac) { unsigned int null_mac[MAC_LEN] = {0, 0, 0, 0, 0, 0}; return (!(mac && (memcmp(mac, null_mac, sizeof(null_mac)) != 0))); } int main() { int ret = 0; char *get_mac = "01005E010106"; //char *get_mac = "000000000000"; unsigned int mac_addr[MAC_LEN] = {0}; char final[18] = "0"; sscanf(get_mac, "%02X%02X%02X%02X%02X%02X \t", &mac_addr[0],&mac_addr[1],&mac_addr[2],&mac_addr[3],&mac_addr[4],&mac_addr[5]); ret = is_null_mac(mac_addr); snprintf(final, sizeof(final),"%02X:%02X:%02X:%02X:%02X:%02X \t", mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]); if (ret == 1) { printf("Mac:[%s] is NULL \n", final); } else { printf("Mac:[%s] is not NULL \n", final); } return 1; }
Output:
Mac:[01:00:5E:01:01:06] is not NULL
|
Comments
Post a Comment