I'm having a heck of a time converting a MAC address to a uint32_t value. I'm new to C so please pardon my lack of understanding.
Some background. This is a datalogger. Here's the process...
arduino powers up and sends a broadcast message via xbee-api
RPi receives the xbee broadcast packet (via an asynchronous python script) and sends a unicast message back to the MAC address of the arduino
the arduino receives a packet with a bunch of data and sets global variables and goes about datalogging, etc
Let's say the datalogger receives this char array:
"1439316593:0013a20040DCCD01:5:300:60:10800:900"
I delimit by ":" and get the MAC address. No problem there.
So now I have a simple char array of "0013a20040DCCD01" and want to pass that to the serialHigh and serialLow of a function and send a unicast packet to that address.
I've removed portions of the code that are irrelevant. This works:
However, due to the dynamic nature of the datalogger, I can't hard code a MAC address in the code - it needs to be dynamic.
So how does one go from a char of "0013a20040DCCD01" to the sH and sL uint32_t values I need?
I've tried too many things to mention without any success. I can delimit values, get values based on sub strings, etc but I'm having trouble understanding how to convert between data types.
uint32_t sH = 0;
uint32_t sL = 0;
char *MAC = (the address of the first character of the MAC address in the string);
for (int i=0; i<16; i++) {
unsigned char nibble = *MAC++;
if (nibble >= '0' && nibble <= '9') {
nibble -= '0';
{
else if (nibble >= 'A' && nibble <= 'F') {
nibble -= 'A';
nibble += 0xA;
}
// nibble now contains the binary value of the current hex characters
if (i<8) {
// First 8 nibbles go into sH
sH <<= 4;
sH |= nibble;
} else {
// Last 8 nibbles go into sL
sL <<= 4;
sL |= nibble;
}
}
John, my goodness, you're amazing! Almost working. There was a small typo in your code (for bracket was off). I corrected the bracket so it would compile (and may have butchered your code), but something's still off. The sL is calculated correctly, just not the sH.
for (int i = 0; i < 16; i++) {
unsigned char nibble = *MAC++;
if (nibble >= '0' && nibble <= '9') {
nibble -= '0';
}
else if (nibble >= 'A' && nibble <= 'F') {
nibble -= 'A';
nibble += 0xA;
}
// nibble now contains the binary value of the current hex characters
if (i < 8) {
// First 8 nibbles go into sH
sH <<= 4;
sH |= nibble;
} else {
// Last 8 nibbles go into sL
sL <<= 4;
sL |= nibble;
}
}
These are the values your snippet figured out:
high: 171200
low: 40DCCD01
Again, apologies I'm not quite there yet to troubleshoot your code, but it's close. Any ideas?
christop - Yes, this is a valid MAC for xbee hardware devices. You're probably thinking of an ethernet MAC address.
char *MAC = "0013A20040DCCD01";
for (int i = 0; i < 16; i++) {
unsigned char nibble = *MAC++;
if (nibble >= '0' && nibble <= '9') {
nibble -= '0';
}
else if (nibble >= 'A' && nibble <= 'F') {
nibble -= 'A';
nibble += 0xA;
}
Serial.print("nibble: ");
Serial.println(nibble);
// nibble now contains the binary value of the current hex characters
if (i < 8) {
// First 8 nibbles go into sH
sH <<= 4;
sH |= nibble;
} else {
// Last 8 nibbles go into sL
sL <<= 4;
sL |= nibble;
}
}
uint32_t sL = 0;
char *MAC = (the address of the first character of the MAC address in the string);
for (int i=0; i<16; i++) {
unsigned char nibble = *MAC++;
if (nibble >= '0' && nibble <= '9') {
nibble -= '0';
{
else if (nibble >= 'A' && nibble <= 'F') {
nibble -= 'A';
nibble += 0xA;
}
// nibble now contains the binary value of the current hex characters
if (i<8) {
// First 8 nibbles go into sH
sH <<= 4;
sH |= nibble;
} else {
// Last 8 nibbles go into sL
sL <<= 4;
sL |= nibble;
}
}
Hi John.
Sorry I'm late for this post. But I need some help. How can this MAC address be saved in a char array? In the NodeDiscovery function, every Router sends back their MAC Address inside an API frame, so I'm able to read them. But every time I want to print them, I see this:
013FFFFFFA2040FFFFFFA1FFFFFFF246 instead the real Address: 0013A20040A1F246
char addr[17];
-Is this 'char addr[17]' the correct way to store the 64Bit Mac Address?