-2

I am trying to convert 64 bit zigbee address into its equivalent hex address. I have gone through many post but could not find any solution. I have 64 bit address in string format and I want it into its equivalent hex.

For example, I have

unsigned char ZbAddStr[16] = "0013A20040B5A5EE"

and I want to convert it into

unsigned char ZbAddNew[8] = {0x00, 0x13, 0xA2, 0x00, 0x40, 0xB5, 0xA5, 0xEE}.

How can I do this transformation?

0

1 Answer 1

2
  1. Copy two bytes of ZbAddStr to a temporary string.

  2. Use strtol to extract the number out of those two bytes using 16 as the base.

  3. Save the number to ZbAddNew.

This should work:

char temp[3] = {0};
for (int i = 0; i < 16; i += 2 )
{
   temp[0] =  ZbAddStr[i];
   temp[1] =  ZbAddStr[i+1];
   int num = strtol(temp, NULL, 16);
   ZbAddNew[i/2] = num;
}

This should also work (Thanks @JonathanLeffler):

for (int i = 0; i < 8; ++i )
{
   sscanf((char*)&ZbAddStr[2*i], "%2X", &ZbAddNum[i]);
}
4
  • 1
    Would there be any major disadvantage to using sscanf(), possibly with a monster format string "%2X%2X…%2X%2X" and 8 addresses, or in a loop for (int i = 0; i < 8; i++) sscanf(&ZbAddStr[2*i], "%2X", &ZbAddNew[i]);? Mar 2, 2015 at 6:42
  • @JonathanLeffler, I didn't try the first suggestion but the second one works for me. I don't see any major pros/cons, subtle or otherwise, between the two approaches.
    – R Sahu
    Mar 2, 2015 at 6:53
  • @CoolNik, good for you.
    – R Sahu
    Mar 4, 2015 at 4:42
  • Thank you for the answers.. I have achieved it using following code.. It does not call any standard function. Code link : goo.gl/GwXH3x
    – CoolNik
    Mar 4, 2015 at 4:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.