How to visualize bytes with C/C++

后端 未结 7 703
小蘑菇
小蘑菇 2020-12-15 10:16

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 visualiz

7条回答
  •  时光取名叫无心
    2020-12-15 11:06

    Just for completeness, a C++ example:

    #include 
    
    template 
    void print_bytes(const T& input, std::ostream& os = std::cout)
    {
      const unsigned char* p = reinterpret_cast(&input);
      os << std::hex << std::showbase;
      os << "[";
      for (unsigned int i=0; i(*(p++)) << " ";
      os << "]" << std::endl;;
    }
    
    int main()
    {
      int i = 12345678;
      print_bytes(i);
      float x = 3.14f;
      print_bytes(x);
    }
    

提交回复
热议问题