converting address to string

How can I veiw the address of a location, that is to say how can I convert an adress to a string or some other type

Answer this question

converting address to string

  • F. Gsell

    Given:

    int i(99), *pi=&i;

    You can output the address directly using:

    std::cout << i << ' ' << pi << '\n';

    Or you can convert to a string using ostringstream as in:

    std::ostringstream ss;
    ss << pi;
    std::cout << ss.str() << '\n';

    Another way to convert to a string is _ultoa or _ultoa_s, but this does not provide leading zeros:

    char Buffer[16];
    _ultoa(reinterpret_cast(pi), Buffer, 16);
    std::cout << Buffer << '\n';
    


  • Glenn Wilson

    thank you for taking the time to anwer my question, but when I try to make a ostringstream it gives me the following error:

    error C2079: 'sc' uses undefined class 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'

    with

    [

    _Elem=char,

    _Traits=std::char_traits<char>,

    _Alloc=std::allocator<char>

    ]


  • comspy

     NeederOfVBHelp wrote:

    I already included the following:

    #include "stdafx.h"

    #include <iostream>

    #include <fstream>

    What am I missing

    #include <sstream>. Reading the documentation helps occasionally



  • Gustis

    Just as a side note you can have a look at some (not so intuitive, but handy) code of mine at http://einaros.blogspot.com/2006/11/put-hex-on-that-dump.html.

  • BobH

    Thank you for your help, I have another related question if it's not to much bother:

    is there way I can convert the address of what a managed handle points to to a string


  • TeleRiddler

    I already included the following:

    #include "stdafx.h"

    #include <iostream>

    #include <fstream>

    What am I missing


  • Sniper167

    Just as for all the STL classes, you need to do the relevant #include.

  • converting address to string