How to get a MAC address from a string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.