jeudi 4 avril 2019

Convert string of digits to string of characters (bytes)

I have the following string: "123456" and I would like to convert it to other string which contains encoded digits: "\x0c\x22\x38" I have omitted here input data validation. Here is the code:

const std::string input = "123456";
std::string output;

const auto size = input.size();
auto pos = 0;
const auto pieceSize = 2;

while(pos < size)
{
    auto substr = input.substr(pos, 2);
    std::uint8_t piece = std::stoi(substr);
    output += piece;
    pos += pieceSize;
}

Do you know any other way to perform such conversion ?

Aucun commentaire:

Enregistrer un commentaire