We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

turn address into char *

Booth882
Booth882 asked
on
Medium Priority
331 Views
Last Modified: 2010-04-02
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
Comment
Watch Question

Commented:

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


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

PS: The answer of elfie will only PRINT the characters, but you will not have the actual string afterwards!

Commented:
>> The answer of elfie will only PRINT the characters
Check the subtle difference between printf() and sprintf().

Author

Commented:
yes but I still get the error

itoa cannot convert parameter 1 from class MyClass * to int

when I do it that way

Author

Commented:
what does that mean "only print the characters"?  what does printf() do in addition of sprintf()?

Author

Commented:
I mean, what does printf do that sprintf doesnt do?

Author

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

Commented:
>> 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.

Commented:
{
   MyClass *MCptr = new MyClass;
    char Base10[20];
    char Base16[20];
    _itoa( *(int *)&MyClass, Base16, 16);
}

Commented:
>> 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?

Commented:
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.                          //
   }
}

Commented:
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);
}

Commented:
MyClass *MCptr = new MyClass;
char Base16[20];
sprintf(Base16, "0x%08x", (int)MCptr);
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thank you all

Commented:
%p's output is not exactly what Booth882 describes.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.