代码之家  ›  专栏  ›  技术社区  ›  OhioDude

如何用C/C可视化字节++

c c++
  •  11
  • OhioDude  · 技术社区  · 17 年前

    我正在努力完成一些C++培训。到目前为止一切顺利,但我需要一些帮助来强化我正在学习的一些概念。我的问题是如何将我创建的对象的字节模式可视化。例如,我如何打印出结构体、long、int等的字节模式?

    我能在脑海中理解它,也能理解我学习材料中的图表,我只是想能够在我的一些学习程序中以编程方式显示字节模式。

    谢谢。

    编辑:我在其他开发项目中主要使用XCode,但有适用于Windows7和fedora核心的VM。在工作中,我使用XP和visual studio 2005。 (我不能评论,因为我在这里仍然是n00b:D)

    我使用了放松的解决方案,这正是我想要的。我也在想,也许我可以使用dos DEBUG命令,因为我也想查看内存块。再一次,这只是为了帮助我巩固我正在学习的东西。再次感谢大家!

    7 回复  |  直到 17 年前
        1
  •  26
  •   vulcan raven    5 年前

    您可以使用这样的函数来打印字节:

    static void print_bytes(const void *object, size_t size)
    {
    #ifdef __cplusplus
      const unsigned char * const bytes = static_cast<const unsigned char *>(object);
    #else // __cplusplus
      const unsigned char * const bytes = object;
    #endif // __cplusplus
    
      size_t i;
    
      printf("[ ");
      for(i = 0; i < size; i++)
      {
        printf("%02x ", bytes[i]);
      }
      printf("]\n");
    }
    

    用法如下,例如:

    int x = 37;
    float y = 3.14;
    
    print_bytes(&x, sizeof x);
    print_bytes(&y, sizeof y);
    

    这将字节显示为原始数值,以十六进制表示,通常用于像这样的“内存转储”。

    在运行“Intel(R)Xeon(R)”CPU的随机(据我所知,甚至可能是虚拟的)Linux机器上,打印:

    [ 25 00 00 00 ]
    [ c3 f5 48 40 ]
    

    这也很容易证明英特尔系列CPU确实 little endian .

        2
  •  6
  •   anon anon    17 年前

    如果你使用gcc和X,你可以使用 DDD debugger 为您绘制数据结构的精美图片。

        3
  •  6
  •   juanchopanza    13 年前

    为了完整起见,举一个C++示例:

    #include <iostream>
    
    template <typename T>
    void print_bytes(const T& input, std::ostream& os = std::cout)
    {
      const unsigned char* p = reinterpret_cast<const unsigned char*>(&input);
      os << std::hex << std::showbase;
      os << "[";
      for (unsigned int i=0; i<sizeof(T); ++i)
        os << static_cast<int>(*(p++)) << " ";
      os << "]" << std::endl;;
    }
    
    int main()
    {
      int i = 12345678;
      print_bytes(i);
      float x = 3.14f;
      print_bytes(x);
    }
    
        4
  •  2
  •   TimW    17 年前

    或者,如果你有boost库并想使用lambda求值,你可以这样做。..

    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";
    }
    
        5
  •  2
  •   Dolphin    17 年前

    大多数(可视化)调试器都有一个“查看内存”选项。Xcode中的IIRC非常基本,只显示HEX和ASCII格式的字节,行长可变。visual Studio(Vs2008中的Debug->Windows->Memory)可以将十六进制部分格式化为不同的整数长度或浮点数,更改字节数,并显示ANSI或UNICODE文本。您还可以设置窗口宽度的任何数字(我认为Xcode只允许您达到64字节宽)。我在这里工作的另一个IDE有很多选项,尽管没有VS那么多。

        6
  •  0
  •   Scott Jasin    7 年前

    我创建了一个一点一点的控制台程序,希望它能帮助到别人

    #include <iostream>
    #include <inttypes.h>
    #include <vector>
    using namespace std;
    typedef  vector<uint8_t> ByteVector;
    ///////////////////////////////////////////////////////////////
    uint8_t Flags[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
    void print_bytes(ByteVector Bv){
        for (unsigned i = 0; i < Bv.size(); i++){
            printf("Byte %d [ ",i);
            for (int j  = 0;j < 8;++j){
                Bv[i] & Flags[j] ? printf("1") : printf("0");
            }
            printf("]\n");
        }
    }
    int main(){
        ByteVector Bv;
        for (int i = 0; i < 4; ++i) { Bv.push_back(i); }
        print_bytes(Bv);
    }
    
        7
  •  -2
  •   jrharshath    17 年前

    试试这个:

    MyClass* myObj = new MyClass();
    int size=sizeof(*myObj);
    int i;
    char* ptr = obj; // closest approximation to byte
    for( i=0; i<size; i++ )
        std::cout << *ptr << endl;
    

    干杯,

    jrh。