如何在C+;中创建十六进制转储实用程序+;? 基本上,我需要用C++编写一个十六进制转储工具。看起来像这样

如何在C+;中创建十六进制转储实用程序+;? 基本上,我需要用C++编写一个十六进制转储工具。看起来像这样,c++,hex,binaryfiles,dump,utility,C++,Hex,Binaryfiles,Dump,Utility,(使用Visual Studio的Word文档十六进制转储的一部分) 我想提示用户输入文件名,然后显示十六进制值以及转换后的ASCII字符。我对处理二进制文件还是新手,所以如果你能保持简单,我将不胜感激。我通常不会为你的这类问题这样做。。。。但不需要花太多的时间就能搞定这样的事情,也许你可以从中学习。这里有一个简单的程序,它只读取标准输入和输出,格式与您展示的大致相同。试试看 代码 #include <iostream> #include <iomanip> using

(使用Visual Studio的Word文档十六进制转储的一部分)


我想提示用户输入文件名,然后显示十六进制值以及转换后的ASCII字符。我对处理二进制文件还是新手,所以如果你能保持简单,我将不胜感激。

我通常不会为你的这类问题这样做。。。。但不需要花太多的时间就能搞定这样的事情,也许你可以从中学习。这里有一个简单的程序,它只读取标准输入和输出,格式与您展示的大致相同。试试看

代码

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    unsigned long address = 0;
    
    cout << hex << setfill('0');
    while( cin.good() )
    {
        int nread;
        char buf[16];
        
        for( nread = 0; nread < 16 && cin.get(buf[nread]); nread++ );
        if( nread == 0 ) break;
        
        // Show the address
        cout << setw(8) << address;

        // Show the hex codes
        for( int i = 0; i < 16; i++ )
        {
            if( i % 8 == 0 ) cout << ' ';
            if( i < nread )
                cout << ' ' << setw(2) << (unsigned int)(unsigned char)buf[i];
            else 
                cout << "   ";
        }

        // Show printable characters
        cout << "  ";
        for( int i = 0; i < nread; i++)
        {
            if( buf[i] < 32 ) cout << '.';
            else cout << buf[i];
        }
        
        cout << "\n";
        address += 16;
    }
    return 0;
}
输出

Hello there, this is a test binary file.
What do you think?

.
00000000  48 65 6c 6c 6f 20 74 68  65 72 65 2c 20 74 68 69  Hello there, thi
00000010  73 20 69 73 20 61 20 74  65 73 74 20 62 69 6e 61  s is a test bina
00000020  72 79 20 66 69 6c 65 2e  0a 57 68 61 74 20 64 6f  ry file..What do
00000030  20 79 6f 75 20 74 68 69  6e 6b 3f 0a 0a 2e         you think?...
#包括
#包括
#包括
#包括
模板
容器类型排列字节(常量字节类型*缓冲区,常量std::size\u t size,常量std::size\u t w=16){
return std::accumulate(buffer,buffer+size,容器类型{{},[w](auto&acc,const字节类型字节){
如果(根据背面()尺寸()>=w){
acc.push_back({});
}
acc.back().推回(字节);
返回acc;
});
}
std::字符串初始文本行(常量整数偏移){
std::ostringstream ost{};

ost那么你想知道如何读入一个文件或者如何将字节值显示为十六进制字符字符串吗?或者你想让别人给你写那个程序吗?第二件事。我知道如何读入一个文件,但一旦我有了二进制文件,我就不知道如何使用它。啊,好的,那么你需要使用一个缓冲区。谢谢你的帮助。什么是
c
呢?--只是吉祥物?;-)此代码取决于是否对默认字符类型进行签名。
#include <iostream>
#include <vector>
#include <iomanip>
#include <numeric>

template<typename byte_type = std::uint8_t, typename container_type = std::vector<std::vector<byte_type>>>
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<typename byte_type = std::uint8_t>
std::string format_row(const std::vector<byte_type>& 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<unsigned>(byte);
      return acc + ost.str();
  });
}

template<typename byte_type = std::uint8_t, typename container_type = std::vector<std::vector<byte_type>>>
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<typename byte_type = std::uint8_t>
std::string hexdump(const byte_type* buffer, std::size_t size) {
  return format_bytes(arrange_bytes(buffer, size));
}

#include <cstring>

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;
}