I am not sure how to get a string from an address in C++.
Pretend this is the address: 0x00020348 Pretend this address holds the value "delicious"
How would I get the string "delicious" from the address 0x00020348? Thank you.
I am not sure how to get a string from an address in C++.
Pretend this is the address: 0x00020348 Pretend this address holds the value "delicious"
How would I get the string "delicious" from the address 0x00020348? Thank you.
Copyright © 2021 Jogjafile Inc.
This answer is to help expand on our dialogue in the comments.
Please see the following code as an example:
Part 1 - The variable
pszSomeStringshould represent the real string in memory you are trying to seek (an arbitrary value but0x00020348for sake of your example).Part 2 - You mentioned that you were storing the pointer value as an
int, soiIntVersionOfAddressis an integer representation of the pointer.Part 3 - Then we take the integer "pointer" and restore it to a
const char* constso that it can be treated as a C-string again.Part 4 - Finally we construct an
std::stringusing the C-string pointer and the length of the string. You wouldn't actually need the length of the string here since the C-string is null character ('\0')-terminated, but I'm illustrating this form of thestd::stringconstructor in the event that you have to logically figure out the length yourself.The output is as follows:
The pointer addresses will vary, but the first three hex pointer values are the same, as would be expected. The new string buffer constructed for the
std::stringversion is a completely different address, also as would be expected.Final note - knowing nothing about your code, a
void*would normally be considered a better representation of a generic pointer than anint.