I have, in a .txt
file, a uint8_t
array already formatted, like this:
0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
What I need is to initialize it from C++ like so:
static const uint8_t binary[] = { 0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, }
Honestly, I'm a bit new at C++.
Is the .txt file storing the hex values as bytes, or as 4 characters representing the hex values with literal commas and spaces?
If you're storing the actual hexadecimal values, the code becomes as simple as
#include <fstream>
#include <vector>
// input file stream
std::ifstream is("MyFile.txt");
// iterators to start and end of file
std::istream_iterator<uint8_t> start(is), end;
// initialise vector with bytes from file using the iterators
std::vector<uint8_t> numbers(start, end);