How to visualize bytes with C/C++

2020-02-09 03:48发布

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!

标签: c++ c
7条回答
啃猪蹄的小仙女
2楼-- · 2020-02-09 04:48

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";
}
查看更多
登录 后发表回答