[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

turn address into char *

Posted on 1998-08-11
17
Medium Priority
?
291 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
0
Comment
Question by:Booth882
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +4
17 Comments
 
LVL 3

Expert Comment

by:elfie
ID: 1169903

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


0
 
LVL 1

Expert Comment

by:willemnel
ID: 1169904
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
 
LVL 1

Expert Comment

by:willemnel
ID: 1169905
PS: The answer of elfie will only PRINT the characters, but you will not have the actual string afterwards!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:alexo
ID: 1169906
>> The answer of elfie will only PRINT the characters
Check the subtle difference between printf() and sprintf().
0
 
LVL 1

Author Comment

by:Booth882
ID: 1169907
yes but I still get the error

itoa cannot convert parameter 1 from class MyClass * to int

when I do it that way
0
 
LVL 1

Author Comment

by:Booth882
ID: 1169908
what does that mean "only print the characters"?  what does printf() do in addition of sprintf()?
0
 
LVL 1

Author Comment

by:Booth882
ID: 1169909
I mean, what does printf do that sprintf doesnt do?
0
 
LVL 1

Author Comment

by:Booth882
ID: 1169910
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169911
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 1169912
{
   MyClass *MCptr = new MyClass;
    char Base10[20];
    char Base16[20];
    _itoa( *(int *)&MyClass, Base16, 16);
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169913
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 1169914
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169915
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
 
LVL 23

Expert Comment

by:chensu
ID: 1169916
MyClass *MCptr = new MyClass;
char Base16[20];
sprintf(Base16, "0x%08x", (int)MCptr);
0
 
LVL 3

Accepted Solution

by:
xyu earned 400 total points
ID: 1169917
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
 
LVL 1

Author Comment

by:Booth882
ID: 1169918
thank you all
0
 
LVL 23

Expert Comment

by:chensu
ID: 1169919
%p's output is not exactly what Booth882 describes.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question