Solved

Displaying a pointer in hex

Posted on 2013-11-29
4
318 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 29

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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
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…

679 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