How would I create a hex dump utility in C++?

前端 未结 2 1376
孤独总比滥情好
孤独总比滥情好 2020-12-15 14:26

Basically, I need to write a hex dump utility using C++. It\'ll look something like this

\"Part

2条回答
  •  忘掉有多难
    2020-12-15 15:02

    #include 
    #include 
    #include 
    #include 
    
    template>>
    container_type arrange_bytes(const byte_type* buffer, const std::size_t size, const std::size_t w = 16) {
      return std::accumulate(buffer, buffer + size, container_type{{}}, [w](auto& acc, const byte_type byte) {
        if(acc.back().size() >= w) {
          acc.push_back({});
        }
        acc.back().push_back(byte);
        return acc;
      });
    }
    
    std::string init_text_row(const int offset) {
      std::ostringstream ost{};
      ost << std::hex << std::setfill('0') << std::setw(8) << offset;
      return ost.str();
    }
    
    template
    std::string format_row(const std::vector& bytes, const int offset) {
      auto init = init_text_row(offset);
      return std::accumulate(bytes.begin(), bytes.end(), init, [](auto& acc, const auto& byte) {
          std::ostringstream ost{};
          ost  << ' ' << std::hex << std::setfill('0') << static_cast(byte);
          return acc + ost.str();
      });
    }
    
    template>>
    std::string format_bytes(const container_type& bytes) {
      struct memory {
        std::string data = {};
        int offset = 0;
      };
      return std::accumulate(bytes.begin(), bytes.end(), memory{}, [](auto& acc, const auto& row) {
        acc.data += format_row(row, acc.offset) + '\n';
        acc.offset += row.size();
        return acc;
      }).data;
    }
    
    template
    std::string hexdump(const byte_type* buffer, std::size_t size) {
      return format_bytes(arrange_bytes(buffer, size));
    }
    
    #include 
    
    int main() {
      const auto* message = "Hello, world! I am Simon the Sourcerer and I am a mighty pirate!";
      const auto len = std::strlen(message);
      std::cout << hexdump(message, len) << std::endl;
      return 0;
    }
    

提交回复
热议问题