[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

turn address into char *

anybody know how to take the address contained in a pointer and change it into a char value like:

int * AddressOfInt == 0x00f45d44;

char * CharacterAddress == { '0','x','0','0','f','4','5','d','4','4' }

it would be a great help.  thank you
0
Booth882
Asked:
Booth882
  • 5
  • 5
  • 2
  • +4
1 Solution
 
elfieCommented:

sprintf(CharacterAddress, "0x%x", AddressOfInt);


0
 
willemnelCommented:
You must remember that a pointer is actually just an integer, hence you can use one of the build in functions. Here's an example of a VC program:


/* ITOA.C: This program converts integers of various
 * sizes to strings in various radixes.
 */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char buffer[20];
   int  i = 3445;
   long l = -344115L;
   unsigned long ul = 1234567890UL;

   _itoa( i, buffer, 10 );
   printf( "String of integer %d (radix 10): %s\n", i, buffer );
   _itoa( i, buffer, 16 );
   printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
   _itoa( i, buffer, 2  );
   printf( "String of integer %d (radix 2): %s\n", i, buffer );

   _ltoa( l, buffer, 16 );
   printf( "String of long int %ld (radix 16): 0x%s\n", l,
                                                    buffer );

   _ultoa( ul, buffer, 16 );
   printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
                                                    buffer );
}

Output:

String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2

You must just remember to use the correct typecasting, something like (unsigned long)AddressOfInt to avoid compiler errors.

Hope this answers your question

0
 
willemnelCommented:
PS: The answer of elfie will only PRINT the characters, but you will not have the actual string afterwards!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
alexoCommented:
>> The answer of elfie will only PRINT the characters
Check the subtle difference between printf() and sprintf().
0
 
Booth882Author Commented:
yes but I still get the error

itoa cannot convert parameter 1 from class MyClass * to int

when I do it that way
0
 
Booth882Author Commented:
what does that mean "only print the characters"?  what does printf() do in addition of sprintf()?
0
 
Booth882Author Commented:
I mean, what does printf do that sprintf doesnt do?
0
 
Booth882Author Commented:
and besides I want to convert the class object address to a
char * that points to the array of characters that makes up the memory address.  tell me how to do that
0
 
nietodCommented:
>> what does printf() do in addition of sprintf()
sprintf() creates a nicely formatted string of ASCII characters that displays the information you requested.  printf() does the same thing, but it prints the information to the screen rather than returning a string with the information.
0
 
nietodCommented:
{
   MyClass *MCptr = new MyClass;
    char Base10[20];
    char Base16[20];
    _itoa( *(int *)&MyClass, Base16, 16);
}
0
 
nietodCommented:
>> besides I want to convert the class object address to a char *
>> that points to the array of characters that makes up the memory
>> address

The way we have been interpreting your question is that you want to print out the address that an object is located at.  That is, you want to a string that expresses the 32-bit number (probably 32) that specifies the location of a object, using a pointer to the object.  If so, the code I just posted should work.  However, there might be a different interpretation.  Do you want a string that expresses the bytes that the object  is stored in in memory.  i.e. do you want a string that looks like a debugger's memory dump of an object?
0
 
nietodCommented:
If you want to express the memory use to store an object you would use

{
    char MemStr[2*sizeof(MyClass) +1];
    MyClass *MCPtr = new MyClass;
    QMthP2A(MemStr,MCPtr,2*sizeof(MyClass));
}

//------------------------------------------------------------------------------------------------//
// PROC:  PUBLIC                                                                                  //
// TYPE:  MSC                                                                                     //
// TITLE: Unpack Data to ASCI                                                                     //
//      This procedure unpacks an array of packed bytes to an array of unpacked ASCI characters.  //
// That is, the value in each nibble of the source array is converted to an ASCI character and    //
// stored in the destination array.  Thus, the 2 byte array 0x12,0x34 would be converted to the 4 //
// byte character array "1234".                                                                   //
//                                                                                                //
//      Binary nibble values from 0 to 9 are converted to the ASCI digits.  Binary nibble values  //
// from 10 to 15 are converted to the uppercase letters "A" to "F".  Thus, the unpacked string    //
// can be interpreted as the hexadecimal representation of the binary data.  For example,         //
// unpacking the 2 byte array 0x1A,0x2B into 4 bytes produces "1A2B".                             //
//                                                                                                //
//      The length specified is the number of characters to be returned.  If this value is even,  //
// then the number of bytes in the source array must be half of this value and each nibble in the //
// source will be unpacked.  If this value is odd, the source array must be half this value       //
// rounded up and the high nibble of the first byte will be ignored.  Thus unpacking the 2 byte   //
// array 0x56,0x78 into a 3 character array would yield "678".                                    //
void
QMthP2A(void       *Dst,                         // -> destination buffer.  Must be at least the  //
                                                 // size specified by the Cnt parameter.          //
        const void *Src,                         // -> source buffer.  Must be at least half the  //
                                                 // number of bytes (rounded up) specified by the //
                                                 // Cnt parameter.                                //
        size_t      Cnt)                         // Number of unpacked bytes to return.  If odd   //
                                                 // the high nibble of the first byte will be     //
                                                 // skipped.  May be zero.                        //
{
   uByt *D     = (uByt *) Dst;                   // -> destination bytes.                         //
   uByt *S     = (uByt *) Src;                   // -> source bytes.                              //
   uByt  u;                                      // Generic value.                                //
   uByt  Cnv[] = "0123456789ABCDEF";             // Binary to ASCI conversion array.              //

   if (Cnt & 1)                                  // If count is odd, then                         //
   {
      u = *S++;                                  // Get source byte.                              //
      *D++ = Cnv[u & 0x0F];                      // Set destination byte from low nibble.         //
      --Cnt;                                     // Decrement the count.                          //
   }
   while (Cnt)                                   // While there are more destination bytes.       //
   {
      u = *S++;                                  // Get source byte.                              //
      *D++ = Cnv[u >> 4];                        // Set destination byte from high nibble.        //
      *D++ = Cnv[u & 0x0F];                      // Set destination byte from low nibble.         //
      Cnt -= 2;                                  // Decrement the count.                          //
   }
}

0
 
nietodCommented:
Sorry for all these posts!  I made a mistake in my other code, it should read
{
       MyClass *MCPtr = new MyClass;
      char Base10[20];
      char Base16[20];
     _itoa( *(int *)&MCPtr, Base10, 10);
     _itoa( *(int *)&MCPtr, Base16, 16);
}
0
 
chensuCommented:
MyClass *MCptr = new MyClass;
char Base16[20];
sprintf(Base16, "0x%08x", (int)MCptr);
0
 
xyuCommented:
I don't understand what are these nice and big things are stand for...
by standard nobody promised that pointer has the same size and the same format as integer... take for example DOS pointers with different segment value taht can point to the same place...

There is a standard way to get string representation of pointer on any OS and its sprintf or printf with format %p !!!

int i;
char szCharAddress[1024]; // hehe :)
sprintf(szCharAddress, "%p", &i);

that will work on any OS, and on any C or C++ compiler and represent the pointer string in a way appropriate to the OS and compiler used. Global suggestion: Read the ANSI standards (C) and Drafts (C++) don't trust the books like "Visual C in 21 day" :)
0
 
Booth882Author Commented:
thank you all
0
 
chensuCommented:
%p's output is not exactly what Booth882 describes.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now