I have a C++ code which will take a pointer address as an argument. The code arguments are:
./main 0x7fad529d5000
Now when reading the arguments, this value will be read as a string.
How do I convert the string "0x7fad529d5000" into an address?
Read a hexadecimal from stdin:
uintptr_t x;
std::cin >> std::hex >> x;
Read a hexadecimal from string:
uintptr_t x;
// assuming you used argc / argv and checked argc > 1
std::istringstream sstr( argv[1] );
sstr >> std::hex >> x;
An alternative would be x = std::stoll( argv[1] );
but there is a cast involved there that I do not quite like (assuming the width of uintptr_t
).
Converting it to a pointer:
void * p = reinterpret_cast<void *>(x);
Handle that pointer value with care, because it is unlikely to be valid. (reinterpret_cast
is something that should scream "danger!" at you.)