jeudi 9 avril 2020

Convert string representation to byte array

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};
}

Aucun commentaire:

Enregistrer un commentaire