12345678910111213141516171819202122232425
#include <iostream> #include <string> #include <type_traits> // assumes that the address stored in the string is valid and is in hex // assumes that T is an (optionally cv-qualified) object type or void template < typename T = const void > T* as_pointer_to( const std::string& address ) { static_assert( std::is_object<T>::value || std::is_void<T>::value, "invalid type" ) ; // http://en.cppreference.com/w/cpp/string/basic_string/stoul const unsigned long long value = std::stoull( address, nullptr, 16 ) ; // may throw return reinterpret_cast< const T* >(value) ; } int main() { const std::string str = "000000000018FB74" ; const void* pv = as_pointer_to<const void>(str) ; std::cout << pv << '\n' ; const int* pi = as_pointer_to<const int>(str) ; std::cout << pi << '\n' ; }