簡體   English   中英

C++ 將從文件讀取的二進制數據轉換為char指針

[英]C++ Convert the binary data read from a file to a char pointer

我正在嘗試實現將從文件讀取的二進制內容連續轉換為字節。 我的文件samplefile.bin包含16 個字節,這是二進制形式。 Hexed.it 中它會像這樣顯示。

在此處輸入圖像描述

在這種情況下我需要的是我需要以十六進制格式提取這些二進制值並將其存儲到char *

我將在這里發布我的示例代碼。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
   uint8_t *ptr;
   long size;
   ifstream file ("samplefile.bin", ios::in|ios::binary|ios::ate);
   size = file.tellg();
   std::vector<uint8_t> memblock(size);
   file.seekg (0, ios::beg);
   file.read(reinterpret_cast<char*>(&memblock[0]), size);
   file.close();
   ptr = new uint8_t[size];
   int z=0;
   for(int i=0; i < memblock.size(); i++)
   {
     std::cout << memblock.at(i) << ' ';
     z=memblock.at(i)&0xff;
     ptr[i] = z;
   }

   cout<<"Output"<<endl;
   for (int i=0;i<memblock.size();i++)
   {
       cout<<ptr[i];
   }
   delete []ptr;

return 0;

}

我希望將ptr傳遞給其他一些 function。但是此打印cout<<ptr[i]<<endl; 如下所示,這意味着轉換沒有發生。

╠ ↕ ‼ ¶ ╙ O N ╥ í * : J Z [ \ J

我想要像下面這樣的東西

ptr[0] = 0xCC
ptr[1] = 0x12
...
ptr[15] = 0x4A

當我這樣給出時cout<<(int)ptr[i]<<endl; ,我得到的0xCC的十進制值為204 ,其他印刷品也是如此。

我已經提到C++ read binary file and convert to hexConvert binary file to hex notation 我沒有找到確切的解決方案,因為在這兩個鏈接中,他們將其轉換為字符串。 我的意圖不是這個。

我提到了這個How properly to read data from binary file to char array這看起來與我的相似。 但這並沒有解決我的問題。
我希望這個0xCC作為一個字節並且應該存儲到ptr[0]並且對於其他值也類似

如何解決這個問題? 我在這里錯過了什么嗎? 我是C++的新手,請幫我解決這個問題

感謝您的努力。

讀取文件並將其數據復制到 char 向量中。 您可以使用像 std::hex 或 std::uppercase 這樣的 iomanip 將 integer 值打印成十六進制。

不建議分配原始指針並將其傳遞給另一個 function。 只需傳遞一個 char 向量或使用 unique_ptr,它們就會避免 memory 問題。

#include <iostream>
#include <iomanip>

std::ifstream file("samplefile.bin", ios::binary);
auto mem = std::vector<char>(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
for (char c : mem)
{
    std::cout << std::uppercase << std::hex << static_cast<int>(c) << endl;
}

您的代碼中存在多個問題,最根本的事實是您似乎期望std::byte8_t在饋送到 output stream 時默認表示為 hexdump。事實並非如此。 您需要明確告訴 stream 為您執行必要的格式化。 如果您想存儲該表示,您還需要存儲流式處理結果。

條條大路通羅馬,不幸的是 C++ 標准 IO stream 庫並沒有使字節流的工作非常符合人體工程學。 但這是一種方法:

#include <cstdint>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

using byte = std::uint8_t;

auto read_bytes(std::istream& is, std::vector<byte>& bytes) -> std::istream& {
    return is.read(& reinterpret_cast<char&>(bytes[0]), bytes.size());
}

auto hexdump(std::vector<byte> const& bytes) -> std::string {
    auto ostr = std::ostringstream();
    ostr << std::uppercase << std::hex << std::setw(2) << std::setfill('0');
    for (auto const c : bytes) {
        ostr << int{c};
    }
    return ostr.str();
}

int main(int argc, char** argv) {
    if (argc != 2) return 1;

    auto fs = std::ifstream(argv[1], std::ios_base::binary | std::ios_base::ate);
    auto const size = fs.tellg();
    fs.seekg(0);
    auto buffer = std::vector<byte>(size);
    if (not read_bytes(fs, buffer)) return 1;

    std::cout << hexdump(buffer) << "\n";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM