There is a string "0A12110023 ...". It is necessary to convert it into a byte array of the following form: 0x0A 0x12 0x11 0x00 0x23 and so on

  • And where is the piece of code you wrote yourself? - sys_dev
  • four
    1) read the input string of 2 characters 2) translate these characters into a number ( "0A" => 0x0A etc) 3) write the number to an array 4) change the C++ tag to C :-) - PinkTux

2 answers 2

Everything, the solution was found:

 char *hexstring = "deadbeef10203040b00b1e50"; int i; unsigned int bytearray[12]; uint8_t str_len = strlen(hexstring); for (i = 0; i < (str_len / 2); i++) { sscanf(hexstring + 2*i, "%02x", &bytearray[i]); } 
  • uint8_t str_len o_O - Qwertiy

Until the tag has been replaced with I propose an appropriate solution:

 #include <string> #include <vector> #include <iostream> #include <iomanip> int main() { const std::string hexs = "deadbeef10203040b00b1e50"; std::vector<int> v; std::cout << std::hex << std::setfill('0'); for(std::string::size_type i = 0; i < hexs.size() / 2; ++i) { v.push_back(std::stoi(hexs.substr(i * 2, 2), nullptr, 16)); std::cout << std::setw(2) << v[i] << " "; } } 

Look at the result