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
2 answers
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_leno_O - Qwertiy ♦
|
Until the c ++ tag has been replaced with c, 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] << " "; } } |
"0A" => 0x0Aetc) 3) write the number to an array 4) change theC++tag toC:-) - PinkTux