The user will input a string value from the terminal. I can represent that as in integer fine. However I need to be able to convert that into a byte array / gsl::span. Is this possible?
#include <string>
#include <iostream>
#include <sstream>
int main()
{
// integer representation
std::string s = "0xaabbccdd";
unsigned int x = std::stoul(s, nullptr, 16);
std::cout << x << "\n";
// byte array or gsl::span
// std::array<uint8_t, 4> val{0xaa, 0xbb, 0xcc, 0xdd};
}
Update possible solution (but using std::vector)
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
std::vector<std::string> split(const std::string & str, int len)
{
std::vector<std::string> entries;
for(std::string::const_iterator it(str.begin()); it != str.end();)
{
int num = std::min(len,(int)std::distance(it,str.end()));
entries.push_back(std::string(it,it+num));
it=it+num;
};
return entries;
}
int main() {
std::string str{"0xaabbccdd"};
str.erase(0, 2);
for (auto str : split(str, 2)) {
std::cout << str << std::endl;
}
return 0;
}