Solved

Displaying a pointer in hex

Posted on 2013-11-29
4
310 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you, All set.  I really appreciate it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

10 Experts available now in Live!

Get 1:1 Help Now