So I have a string that contains a byte array of HEX values.
I am trying to figure out how to go from the string back to the original byte array.
I am using an Arduino-based board.
Simply put, I want to turn this:
char addr3[47] = "0x28, 0xB6, 0x4C, 0x4E, 0x0D, 0x00, 0x00, 0x86";
Back into this:
byte addr2[8] = {0x28, 0xB6, 0x4C, 0x4E, 0x0D, 0x00, 0x00, 0x86};
Thank you for your help.
EDIT, Here is a solution I came up with following the answers/comments:
Starting with:
char addr3[47] = "0x28, 0xB6, 0x4C, 0x4E, 0x0D, 0x00, 0x00, 0x86";
I run the string through this:
sscanf(addr3, "%x", &r1);
sscanf(addr3, "%*s %x", &r2);
sscanf(addr3, "%*s %*s %x", &r3);
sscanf(addr3, "%*s %*s %*s %x", &r4);
sscanf(addr3, "%*s %*s %*s %*s %x", &r5);
sscanf(addr3, "%*s %*s %*s %*s %*s %x", &r6);
sscanf(addr3, "%*s %*s %*s %*s %*s %*s %x", &r7);
sscanf(addr3, "%*s %*s %*s %*s %*s %*s %*s %x", &r8);
byte addr[8] = {r1,r2,r3,r4,r5,r6,r7,r8};
and then we get:
byte addr[8] = 28B64C4ED0086
Which although is not the exact format I was looking for, it will work.
CodePudding user response:
To accomplish this you need to parse the string and convert the individual values to byte. You can do this using the sscanf function in the c library. I don't know if the arduino has an implementation of the sscanf function. But I would look into that first. If it does not then you will have to write a function that reads each character of the hex string and coverts it to the 4bit equivalent.