-1

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.

SammyG
  • 29
  • 4
  • 1
    What did you try? Can you show the code that you wrote, and explain what is the problem with it that you cannot solve? We don't write code for other people, on Stackoverflow, but we can answer questions and point out what any problems might be. – Sam Varshavchik Oct 13 '21 at 23:31
  • 1
    Hint: `std::istringstream`. – Thomas Matthews Oct 13 '21 at 23:32
  • @SamVarshavchik The problem I cannot solve is getting from that string back to the byte array. I have tried using memcpy but I think that's not the right path. I have the feeling I am missing something. Do you know if there's a method for doing this? – SammyG Oct 13 '21 at 23:49
  • @ThomasMatthews Thank you for that hint. Looking into it but not sure if that is available in my Arduino-based environment. – SammyG Oct 13 '21 at 23:53
  • 1
    You may want to also consider: `atoi` and `strotoul`. – Thomas Matthews Oct 14 '21 at 00:19
  • Search for "[c++] hex string to byte array" or https://stackoverflow.com/questions/17261798/ Also you can write `char addr3[] = "..."` the compiler will figure out what size `addr3` should be, or use `std::string` – Barmak Shemirani Oct 14 '21 at 01:42
  • There are many methods of doing this. They are just like all other methods of implementing all other tasks in C++: open a text editor, write the program, try to compile and run it, see if it works, if it doesn't produce the correct results debug it, to determine why, make any necessary fixes, try again. You will need to attempt to implement your program yourself, first, and then ask any questions on Stackoverflow after showing your work. Stackoverflow is not a C++ tutorial site, we just answer ***specific*** technical questions about programming. "How do I do " is not a specific question. – Sam Varshavchik Oct 14 '21 at 02:29

1 Answers1

1

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.