Today, I felt that the programming level down, deliberately practiced, according to the MAC address string, to obtain the MAC address of 2 methods.
#include <stdio.h>
void Func1 (char *str)
{
unsigned char mac[6] = {0};
SSCANF (str, "%2x:%2x:%2x:%2x:%2x:%2x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
printf ("func:%s output:mac=%02x:%02x:%02x:%02x:%02x:%02x \ n", __function__, Mac[0], mac[1], mac[2],mac[3], Mac[4],mac [5]);
return;
}
int GetValue (char c)
{
if ((c >= ' 0 ') && (c <= ' 9 '))
Return C-' 0 ';
else if ((C >= ' a ') && (c <= ' F '))
Return C-' a ' + 10;
else if ((C >= ' A ') && (c <= ' F '))
Return C-' A ' + 10;
Else
return 0;
}
void Func2 (char *str)
{
/*0f:e2:11:ab:cd:03*/
int i;
unsigned char low;
unsigned char high;
unsigned char mac[6] = {0};
for (i=0; i<6; i++)
{
High = GetValue (str[i*3]);
Low = GetValue (str[i*3 + 1]);
Mac[i] = (high<<4) + low;
}
printf ("func:%s output:mac=%02x:%02x:%02x:%02x:%02x:%02x \ n", __function__, Mac[0], mac[1], mac[2],mac[3], Mac[4],mac [5]);
}
int main (int argc, char** argv) {
Func1 (argv[1]);
Func2 (argv[1]);
return 0;
}
Run Result: Two methods are visible for the same result
How to get a MAC address from a string