Solved

Displaying a pointer in hex

Posted on 2013-11-29
4
312 Views
Last Modified: 2013-12-03
MSVS2010 C++ question.
How do I display a pointer in hex in a dialog box.  I have created the variable as a long.
I would like to copy a pointer into the field and coerce it to display in hex mode.
This is 32-bit build.
0
Comment
Question by:supportoranges
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 39687139
Probably the easiest way is to use a stream and output the pointer value casted to void*, like this:
#include <iostream>
using namespace std;
int main()
{
    char sz[] = "some chars";
    cout << static_cast<void *>(sz) << endl;
    return 0;
}

Open in new window

For dialog, you may want to use <sstream> instead:
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    char sz[] = "some chars";

    ostringstream oss;
    oss << static_cast<void *>(sz);

    cout << oss.str() << endl;
    return 0;
}

Open in new window

Here displayed also via cout; however, oss.str() is the string representation of the pointer value. For Windows dialog, you probly need the C-string. Then use oss.str().c_str().

For displaying integer values in hex, include the standard header iomanip that gives you the hex manipulator, setfill('0'), and setw(8). Using them for any stream, the next integer will be displayed in hex representation using 8 characters, left padded by zeros:
#include <iomanip>
    ...
    ostringstream oss;
    oss << << hex << setfill('0') << setw(8) << theValue;

Open in new window

0
 
LVL 1

Author Comment

by:supportoranges
ID: 39690840
Thank you.  I never use these streams but I could.  In your example you output the correct format.  But I don't want to output it to a device.  I want to put the representation back into a CString so that I can do the assignment to a dialog box variable easily.

Would you be so kind as to let me know how I could use your idea but end up with a CString instead of something written to a terminal.  Also not practical for me to redirect streams if you know what I mean.
0
 
LVL 28

Expert Comment

by:pepr
ID: 39691814
The cout is the stream bound to the standard output. However, oss in the example is a stream bound to the memory buffer. The oss (of the ostringstream class) behaves as a stream but can also be accessed as if it were a string object. A string object can use its .c_str() method to return the pointer to char buffer -- which is a zero ended C-string.

The CString class has the constructor that accepts a C-string literal (or the pointer to a zero ended char buffer). This way, you can use ostringstream instance to put the info there, extract the C-string pointer, and use it of CString contructor or assignment:
    ostringstream oss;
    oss << static_cast<void *>(pointer);
    CString s(oss.str().c_str());
    ... or ...
    s = oss.str().c_str()

Open in new window

The only remaining question is whether you use Unicode strings or not.
0
 
LVL 1

Author Closing Comment

by:supportoranges
ID: 39692434
Thank you, All set.  I really appreciate it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now