how to assign address to a pointer where address is store in CString object?
CString str("0x1b1be0")
void* ptr = NULL;
Now i want to assign ptr = 0x1b1be0
开发者_如何学编程. How do I achieve this?
Try this:
CString str("0x1b1be0");
void* ptr = (void *)_tcstoul(str.GetBuffer(), 0, 0);
And this is in pure C:
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *s = "0x1b1be0";
void *p = (void *)strtoul(s, 0, 0);
printf("%p", p);
return 0;
}
Try it on Codepad.
Note: This version has no error checking. Just to demonstrate the idea.
#include <sstream>
std::stringstream s;
s << "0x1234";
void* a;
s >> std::hex >> a;
Here is a code.
#include <sstream>
#include <iostream>
int main() {
void * x;
std::stringstream ss;
ss << std::hex << "0x1b1be0";
ss >> x;
std::cout << x << std::endl;
}
Hmmm, unless you're writing something very low level such as a debugger, having to do such a thing is highly questionable...
Are you sure you know what a pointer means ?
Do you know that pointers are private to your process ? They most often have no meaning outside.
To answer your question, you obviously have to:
- Convert your string to an
intptr_t
using for examplestd::sscanf
with the%x
format specifier. - Assign your pointer with
reinterpret_cast< void* >
.
精彩评论