Hunter
Hunter

Reputation: 3

String containing address of value -> pointer for that value

So I have a string that holds an address to another variable, and I want to cast that address to an actual pointer so I can edit the value at that address.

So, I basically want to do the opposite of this.

int value = 10;
auto address = reinterpret_cast<uintptr_t>(&value);
std::stringstream ss;
ss << std::hex << address;
std::string strAddr (ss.str());
std::cout << "Address of value = " << strAddr << std::endl; // just append "0x" to start if you want to format it like hex

Upvotes: 0

Views: 78

Answers (1)

Thomas Matthews
Thomas Matthews

Reputation: 57678

The reverse process:

const std::string address_as_text = "0x12345678";
unsigned int address_as_number = 0U;
std::istringstream addr_stream(address_as_text);
addr_stream >> hex >> address_as_number;
int * pointer_to_int = (int *) address_as_number;
int value_from_memory = *pointer_to_int;

Upvotes: 1

Related Questions