Solved

Displaying a pointer in hex

Posted on 2013-11-29
4
325 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 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
What algorithm is used to encrypt 33 639
Recursive algorithm 8 249
Lowest common IP of a range of IPs 6 305
Decrease number proportionately 20 100
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…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
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…

751 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