Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Displaying a pointer in hex

Posted on 2013-11-29
4
Medium Priority
?
340 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
[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
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
pepr earned 2000 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 29

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

715 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