// Ivan Novick // Jan-2, 2009 // ptr2string.c // platform independent function to convert a pointer into a string // this will work on 32 or 64 bit linux or windows // the assumptions are that unsigned long long is 8 bytes // and that the pointer is at most 8 bytes long #include void ptr2String(char *dstPtr, void* srcPtr) { unsigned long long address = (unsigned long long)srcPtr; short i; for (i = 15; i >= 0; --i) { short x = (short)(address & 0x0f); char c = x < 10 ? x + '0' : x - 10 + 'a'; dstPtr[i] = c; address >>= 4; } dstPtr[16] = 0; } int main(int argc, char** argv) { int x[8] = {0}; int i; for (i = 0; i < 8; ++i) { char buffer[17] = {0}; ptr2String(buffer, &x[i]); fprintf(stdout, "%s %p\n", buffer, &x[i]); } return 0; }