Click here to Skip to main content
Licence CPOL
First Posted 8 Apr 2009
Views 17,654
Downloads 173
Bookmarked 5 times

Convert MAC Address String into Bytes

By | 8 Apr 2009 | Article
The code snippet converts MAC Address String Format into Bytes

Introduction 

inet_addr and inet_ntoa are already available for IP address processing. Here, I put the same functionalities for MAC address.

This code snippet converts MAC address string into bytes array and bytes array into MAC address' string.  

Background

I searched the internet but I could not find such a function so I publish it here for every one. 

Using the Code

The code is self explanatory. 

const char cSep = '-'; //Bytes separator in MAC address string like 00-aa-bb-cc-dd-ee

/*
	This function accepts MAC address in string format and returns in bytes. 
	it relies on size of byAddress and it must be greater than 6  bytes.
	If MAC address string is not in valid format, it returns NULL.

	NOTE: tolower function is used and it is locale dependent.
*/
unsigned char* ConverMacAddressStringIntoByte
	(const char *pszMACAddress, unsigned char* pbyAddress)
{
	for (int iConunter = 0; iConunter < 6; ++iConunter)
	{
		unsigned int iNumber = 0;
		char ch;

		//Convert letter into lower case.
		ch = tolower (*pszMACAddress++);

		if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
		{
			return NULL;
		}

		//Convert into number. 
		//       a. If character is digit then ch - '0'
		//	b. else (ch - 'a' + 10) it is done 
		//	because addition of 10 takes correct value.
		iNumber = isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
		ch = tolower (*pszMACAddress);

		if ((iConunter < 5 && ch != cSep) || 
			(iConunter == 5 && ch != '\0' && !isspace (ch)))
		{
			++pszMACAddress;

			if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
			{
				return NULL;
			}

			iNumber <<= 4;
			iNumber += isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
			ch = *pszMACAddress;

			if (iConunter < 5 && ch != cSep)
			{
				return NULL;
			}
		}
		/* Store result.  */
		pbyAddress[iConunter] = (unsigned char) iNumber;
		/* Skip cSep.  */
		++pszMACAddress;
	}
	return pbyAddress;
}
/*
	This function converts Mac Address in Bytes to String format.
	It does not validate any string size and pointers.

	It returns MAC address in string format.
*/

char *ConvertMacAddressInBytesToString(char *pszMACAddress, 
				unsigned char *pbyMacAddressInBytes)
{
	sprintf(pszMACAddress, "%02x%c%02x%c%02x%c%02x%c%02x%c%02x", 
						pbyMacAddressInBytes[0] & 0xff,
						cSep,
						pbyMacAddressInBytes[1]& 0xff, 
						cSep,
						pbyMacAddressInBytes[2]& 0xff, 
						cSep,
						pbyMacAddressInBytes[3]& 0xff, 
						cSep,
						pbyMacAddressInBytes[4]& 0xff, 
						cSep,
						pbyMacAddressInBytes[5]& 0xff);

	return pszMACAddress;
} 

History

  • 8th April, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

anand choubey

Software Developer (Senior)
Juniper Networks
India India

Member

I started my programming with DOS. Now I am flying on C# sky.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberAparna_Anand20:45 1 Jul '10  
GeneralSimpler implementation PinmemberDamien Dube5:38 28 Apr '10  
GeneralMy vote of 1 Pinmemberbarto2:44 9 Apr '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 8 Apr 2009
Article Copyright 2009 by anand choubey
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid