Created Fri, 28 Dec 2012 16:56:42 +0000 by lffelzmann@gmail.com
Fri, 28 Dec 2012 16:56:42 +0000
Hello all,
I'm using chipKit Network Shield and I want to set the MAC address. The problem is that I need the mac address in this format:
Example: byte mac[]={0x00,0x18,0x3E,0x01,0x07,0x96};
However, I have this:
Example: unsigned char mac_address[13]="00183E010796";
Remember that this is just an example. In my code, the mac_address is received by a keyboard. It is not a constant. So I cannot just change the second example by the first one.
I want to convert the mac in string format to the byte format. However, I don't know exactly how to do it. Does anyone have a suggestion?
Thank you.
Fri, 28 Dec 2012 18:27:53 +0000
This function will convert a single character into its equivalent 4-bit binary value:
unsigned char h2d(unsigned char hex)
{
if(hex > 0x39) hex -= 7; // adjust for hex letters upper or lower case
return(hex & 0xf);
}
You can combine two characters to make a single 8-bit value:
unsigned char h2d2(unsigned char h1, unsigned char h2)
{
return (h2d(h1)<<4) | h2d(h2);
}
By the way, hexadecimal is just one of many ways of representing an 8-bit binary value. It's purely a human thing. Hexadecimal means nothing to the CPU.
So, you can use those functions to take pairs of characters from the string, and get 6 8-bit values out of it, which you can then use as your MAC address.
I'll let you work that part out.