Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to visualize bytes with C/C++

Tags:

c++

c

I'm working my way through some C++ training. So far so good, but I need some help reinforcing some of the concepts I am learning. My question is how do I go about visualizing the byte patterns for objects I create. For example, how would I print out the byte pattern for structs, longs, ints etc?

I understand it in my head and can understand the diagrams in my study materials, I'd just like to be able to programaticially display byte patterns from within some of my study programs.

I realize this is pretty trivial but any answers would greatly help me hammer in these concepts.

Thanks.

Edit: I use mostly XCode for my other development projects, but have VMs for Windows7 and fedora core. At work I use XP with visual studio 2005. ( I can't comment as I am still a n00b here :D)

I used unwind's solution which is about what I am looking for. I am also thinking that maybe I could just use the dos DEBUG command as I'd also like to look at chunks for memory too. Again, this is just to help me reinforce what I am learning. Thanks again people!

like image 924
OhioDude Avatar asked May 28 '09 11:05

OhioDude


1 Answers

Or if you have the boost lib and want to use lambda evaluations you can do it this way ...

template<class T>
void bytePattern( const T& object )
{
    typedef unsigned char byte_type;
    typedef const byte_type* iterator;

    std::cout << "Object type:" << typeid( T ).name() << std::hex;
    std::for_each( 
        reinterpret_cast<iterator>(&object), 
        reinterpret_cast<iterator>(&object) + sizeof(T), 
        std::cout << constant(' ') << ll_static_cast<int>(_1 )&&0xFF );   
    std::cout << "\n";
}
like image 77
TimW Avatar answered Sep 23 '22 06:09

TimW