[Solved] If I’m able to printf an array index with %x, how do I then save it into a string? [closed]


You have a couple of choices. You can use sprintf or one of it’s cousins which work like printf or you can use std::ostringstream.

snprintf example

#include <cstdio>

char buffer[100] = "some text already there ";
std::snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%x", index);

.

std::ostringstream example

#include <sstream>

std::string text("some already existing text ");
std::ostringstream buffer;
buffer << std::hex << index;
text += buffer.str();

3

solved If I’m able to printf an array index with %x, how do I then save it into a string? [closed]